ios - 像 uber 一样在 Mapbox map 上移动 MGLPointAnnotation

标签 ios swift mapbox mapbox-ios

我正在将 Mapbox iOS SDK 集成到我的应用程序中。 现在我陷入了一个困境,我想实现像 Uber 应用程序这样的汽车跟踪功能。

我曾经在 Google Maps SDK 中使用过该功能,但无法使其与 Mapbox SDK 一起使用。

我正在添加 MGLPointAnnotation 对象以将其添加到 map 上,并希望通过动画将其从 A 点移动到 B 点。

我正在使用

UIView.animate(withDuration: TimeInterval(duration), animations: {
    // Update annotation coordinate to be the destination coordinate
    point.coordinate = newCoordinate
}, completion: nil)

但是对于 MGLPointAnnotation 我无法更改其图像,因为当转弯时我想旋转图像(注释)。

如果我使用 MGLAnnotationView 对象,我可以更改图像,但无法更改其坐标,因为它是只读的。

我应该在这里做什么来实现该功能?

最佳答案

几年前我写过类似的东西,我使用GoggleMaps,浏览一下代码,也许会有帮助,我真的不记得角度计数中的所有数字是什么

    extension Double {

    var degrees: Double {
        return self * 180.0 / Double.pi
    }

}


extension CLLocationCoordinate2D {

    func angleToPosition(position : CLLocationCoordinate2D) -> CLLocationDegrees {
        let bearingRadians = atan2(Double(position.latitude - latitude), Double(position.longitude - longitude))
        var bearingDegrees = bearingRadians.degrees
        //        print("\(bearingDegrees)")
        var roundDegrees = 360.0
        if bearingDegrees < 0 {
            if bearingDegrees > -90 {
                roundDegrees = 350
            }
            if bearingDegrees < -90 && bearingDegrees > -180 {
                roundDegrees = 370
            }
            bearingDegrees += roundDegrees
            return 360 - bearingDegrees
        }
        roundDegrees = bearingDegrees < 90 ? 350 : 360
        if bearingDegrees > 90 && bearingDegrees < 180 {
            roundDegrees = 370
        }
        UserDefaults.standard.set(bearingDegrees, forKey: "bearingDegrees")
        return roundDegrees - bearingDegrees
    }

    func duration(toDestination destination: CLLocationCoordinate2D, withSpeed speed : Double) -> Double {
        let distance = GMSGeometryDistance(self, destination)
        return distance / (speed * (1000 / 3600))
    }

}

这是执行旋转的函数,您可以在收到新坐标后立即使用它,或者如果您有特定的折线,则在 for 循环中调用它

func animateCarDrive(info: [String: Any]) {
    let speed = info["speed"] as? Double ?? 40 // if your car's speed is changeable
    let position = info["position"] as? CLLocationCoordinate2D // new point position
    let duration = marker.position.duration(toDestination: position, withSpeed: speed)
    marker.rotation = marker.position.angleToPosition(position: position)
    CATransaction.begin()
    CATransaction.setAnimationDuration(duration)
    marker.position = position
    CATransaction.commit()
}

关于ios - 像 uber 一样在 Mapbox map 上移动 MGLPointAnnotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247757/

相关文章:

ios - 异步执行但同步写入

iOS:如何使用 SQLite.swift 将第一个运行的应用程序中的预置数据库复制到可写位置?

swift - Swift 中隐式返回使用背后的原因

ios - Swift 中 Google Analytics 异常跟踪的正确语法是什么?

android - 使用 GoogleMap 或 MapBox Direction API 在我的应用程序中实现我自己的导航

ios - 从 UIViewController 获取代码到 UIView

ios - 收到的事件子类型是无法识别的选择器

ios - Swift 4 - AVAudioPlayer 从 UISlider 设置 currentTime

ios - 如果用户拥有没有 watch 扩展的 Apple Watch,如何在 iOS 应用程序中进行跟踪?

Mapbox GL JS : Set base layer to white?