keep zoom when using mkmapcamera issue

305 views Asked by At

I'm newbie on IOS develope app with Swift 3 and Xcode 8. In my app I use regionDidChangeAnimated delegate method to keep zoomLevel in meters by mapView.region.span.latitudeDelta * 111.000.

When new location data is available I use MKMapCamera to show userlocation with fromDistance parameter = mapView.region.span.latitudeDelta * 111.000 previously calculated and saved in instance variable.

The issue is when I pinch in and pinch out zoom level does not work properly

Below post a bit code:

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
        if (mapChangedFromUserInteraction) {
            self.latitudineDelta = Float(mapView.region.span.latitudeDelta)
        }
    }

    func mapViewRegionDidChangeFromUserInteraction() -> Bool {
        let view = self.mapView.subviews[0]
        if let gestureRecognizers = view.gestureRecognizers {
            for recognizer in gestureRecognizers {

                if(recognizer.state == UIGestureRecognizerState.ended)
                {
                    return true
                }
            }
        }
        return false
    }

    fileprivate func centerMapOnLocation()
    {
        let coordinate = CLLocationCoordinate2D(latitude: self.latitude!,longitude: self.longitude!)

        if (mapChangedFromUserInteraction == false)
        {
            let distance: CLLocationDistance = CLLocationDistance(Int(self.latitudineDelta * 111000))
            self.camera = MKMapCamera(lookingAtCenter: coordinate,
                                 fromDistance: distance,
                                 pitch: pitch,
                                 heading: self.heading)
            self.mapView.setCamera(self.camera!, animated: isAnimated)
        }
    }
0

There are 0 answers