Dim
Dim

Reputation: 532

How to detect zoom's effect on the map in Swift

I am using regionDidChangeAnimated for my mapView. I want to detect when user zoom's or drag's a map. Drag is being detected, but I can't seem to detect when user zoom's the map. I tried to use:

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

But when I zoom my map it does not work.

Full code:

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")
}

Upvotes: 3

Views: 3591

Answers (1)

Dim
Dim

Reputation: 532

I have solved it. In the override func viewDidLoad() need to add self.mapView.delegate = self And add functions for determine zoom's on the map:

private var mapChangedFromUserInteraction = false

private func mapViewRegionDidChangeFromUserInteraction() -> Bool {
    let view = self.mapView.subviews[0]
    if let gestureRecognizers = view.gestureRecognizers {
        for recognizer in gestureRecognizers {
            if( recognizer.state == UIGestureRecognizer.State.began || recognizer.state == UIGestureRecognizer.State.ended )) {
                return true
            }
        }
    }
    return false
}

func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
    mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()

}

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    if (mapChangedFromUserInteraction) {
        print("ZOOM finished")
    }
}

Upvotes: 14

Related Questions