使用mkmapcamera问题时保持缩放

时间:2017-01-10 21:08:20

标签: ios mkmapview mkmapcamera

我是使用Swift 3和Xcode 8开发IOS的新手。 在我的应用程序中,我使用regionDidChangeAnimated委托方法通过mapView.region.span.latitudeDelta * 111.000将zoomLevel保持为米。

当新的位置数据可用时,我使用MKMapCamera显示用户位置,其中fromDistance参数= mapView.region.span.latitudeDelta * 111.000先前计算并保存在实例变量中。

问题是当我捏合并缩小缩放级别无法正常工作时

下面发一位代码:

    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 个答案:

没有答案