用一根手指在mapbox地图上缩放似乎还能更改中心位置?

时间:2019-02-15 01:38:46

标签: ios swift mapbox

下面是我的缩放方法。我正在尝试实现它,以便当用户使用一根手指进行缩放(在指定区域内向上或向下移动手指)时,它应该像正常情况一样向下或向上缩放(例如,请参阅snapchat)。

问题是我编写的代码同时更改了纬度,这是意外的。

为什么会发生这种情况,我该如何更改?

    @objc func panGesture(_ sender: UIPanGestureRecognizer) {
    print("recognized?")

    // note that 'view' here is the overall video preview
    let velocity = sender.velocity(in: mapView) //view

    //print(sender.translation(in: view), "<-- waht is val?")
    //        print(sender.setTranslation(CGPoint(x: 16, y: 495), in: view), "<-- mmmmm")

    if velocity.y >= 2 || velocity.y <= 2 {

        let minimumZoomFactor: CGFloat = 1.0
        let maximumZoomFactor: CGFloat = 17.0 // artificially set a max useable zoom of 14x (maybe shuld be icncrease?)

        // clamp a zoom factor between minimumZoom and maximumZoom
        func clampZoomFactor(_ factor: CGFloat) -> CGFloat {
            return min(max(factor, minimumZoomFactor), maximumZoomFactor)
        }

        func update(scale factor: CGFloat) {
            mapView.zoomLevel = Double(exactly: factor)! //maaybe setZoomLevel(... and animate it bit by bit?

        }
        var lastVal = 0

        //BELOW IS SENDER.STATE THINGS!!!
        switch sender.state {

        case .began:

            originalZoomLevel = mapView.zoomLevel//device.videoZoomFactor
            //print(originalZoomLevel, "<-- what is initialZoom11111111???")

        case .changed:

            // distance in points for the full zoom range (e.g. min to max), could be view.frame.height
            let fullRangeDistancePoints: CGFloat = 300.0 //dont know fi this is right??

            // extract current distance travelled, from gesture start
            let currentYTranslation: CGFloat = sender.translation(in: view).y

            // calculate a normalized zoom factor between [-1,1], where up is positive (ie zooming in)
            let normalizedZoomFactor = -1 * max(-1,min(1,currentYTranslation / fullRangeDistancePoints))

            // calculate effective zoom scale to use
            let newZoomFactor = clampZoomFactor(CGFloat(originalZoomLevel) + normalizedZoomFactor /** (maximumZoomFactor - minimumZoomFactor)*/)
            print(originalZoomLevel, "<-- what is initialZoom???")
            print(newZoomFactor, "<-- what is newZoomFactor???")

            // update device's zoom factor'
            update(scale: newZoomFactor)
            print(lastVal - Int(mapView.centerCoordinate.latitude), " : The change is here")

            lastVal = Int(mapView.centerCoordinate.latitude)
            print(mapView.centerCoordinate, " : Cenetr courdenate in .changed")
        case .ended, .cancelled:
            print(originalZoomLevel, "<-- what is this???")
            break

        default:
            break
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您的代码中的问题出在您的func update(scale ...)

替换此代码:

mapView.zoomLevel = Double(exactly: factor)!

与此:

mapView.setCenter(centerCoordOrig, zoomLevel: Double(exactly: factor)!, animated: false)

顺便说一句,在centerCoordOrig外部创建一个新变量。