2016-08-13 6 views
1

내 mapView에 regionDidChangeAnimated을 사용하고 있습니다. 사용자가 확대/축소하거나지도를 끌 때 감지하고 싶습니다. 드래그가 감지되지만 사용자가지도를 확대/축소 할 때 감지 할 수 없습니다. 나는 다음을 사용하려고 시도했다 :Swift에서지도의 확대/축소 효과를 감지하는 방법

func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) { 
    print("region will change") 

} 

그러나지도를 확대/축소하면 작동하지 않는다.

전체 코드 :

var locationManager: CLLocationManager = CLLocationManager() 
var startLocation: CLLocation! 

@IBOutlet weak var mapView: MKMapView! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    //get current location 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.delegate = self 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.startUpdatingLocation() 

    self.mapView.showsUserLocation = true 
    startLocation = nil 

    let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: "didDragMap:") 
    mapDragRecognizer.delegate = self 
    self.mapView.addGestureRecognizer(mapDragRecognizer) 



    self.mapView.pitchEnabled = true 
    self.mapView.showsBuildings = true 

} 
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]) 
{ 
    let location = locations.last 
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)) 
    mapView.setRegion(region, animated: false) 
    let currentLocX = String(format: "%.4f", location!.coordinate.latitude) 
    let currentLocY = String(format: "%.4f", location!.coordinate.longitude) 

    self.locationManager.stopUpdatingLocation() 
} 
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true 
} 

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
    print("Errors: " + error.localizedDescription) 
} 


func didDragMap(gestureRecognizer: UIGestureRecognizer) { 

    print("Drag") 
} 
func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) { 
    print("Zoom") 

} 

답변

5

나는 그것을 해결했다. override func viewDidLoad()에서 self.mapView.delegate = self 을 추가해야합니다. 지도에서 확대/축소를 결정하는 함수를 추가하십시오.