【问题标题】:Move the annotation on Google Map like Uber iOS application in Swift?像 Swift 中的 Uber iOS 应用程序一样在 Google Map 上移动注释?
【发布时间】:2017-05-04 06:34:22
【问题描述】:

我正在尝试开发一个基于 Uber 和 Ola 之类概念的应用程序。因此,为此我需要集成谷歌地图以获取 iOS 的接送位置。所以请告诉我如何使用谷歌地图在iOS中实现移动注释(汽车)动画。

【问题讨论】:

    标签: ios iphone google-maps swift3 xcode8


    【解决方案1】:

    使用 Swift 3.1

            var oldCoodinate: CLLocationCoordinate2D? = CLLocationCoordinate2DMake(CDouble((data.value(forKey: "lat") as? CLLocationCoordinate2D)), CDouble((data.value(forKey: "lng") as? CLLocationCoordinate2D)))
            var newCoodinate: CLLocationCoordinate2D? = CLLocationCoordinate2DMake(CDouble((data.value(forKey: "lat") as? CLLocationCoordinate2D)), CDouble((data.value(forKey: "lng") as? CLLocationCoordinate2D)))
            driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
            driverMarker.rotation = getHeadingForDirection(fromCoordinate: oldCoodinate, toCoordinate: newCoodinate)
            //found bearing value by calculation when marker add
            driverMarker.position = oldCoodinate
            //this can be old position to make car movement to new position
            driverMarker.map = mapView_
            //marker movement animation
            CATransaction.begin()
            CATransaction.setValue(Int(2.0), forKey: kCATransactionAnimationDuration)
            CATransaction.setCompletionBlock({() -> Void in
                driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
                driverMarker.rotation = CDouble(data.value(forKey: "bearing"))
                //New bearing value from backend after car movement is done
            })
            driverMarker.position = newCoodinate
            //this can be new position after car moved from old position to new position with animation
            driverMarker.map = mapView_
            driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
            driverMarker.rotation = getHeadingForDirection(fromCoordinate: oldCoodinate, toCoordinate: newCoodinate)
            //found bearing value by calculation
            CATransaction.commit()
    
    extension Int {
        var degreesToRadians: Double { return Double(self) * .pi / 180 }
    }
    extension FloatingPoint {
        var degreesToRadians: Self { return self * .pi / 180 }
        var radiansToDegrees: Self { return self * 180 / .pi }
    }
    

    新旧坐标获取方位值的方法

    func getHeadingForDirection(fromCoordinate fromLoc: CLLocationCoordinate2D, toCoordinate toLoc: CLLocationCoordinate2D) -> Float {
    
            let fLat: Float = Float((fromLoc.latitude).degreesToRadians)
            let fLng: Float = Float((fromLoc.longitude).degreesToRadians)
            let tLat: Float = Float((toLoc.latitude).degreesToRadians)
            let tLng: Float = Float((toLoc.longitude).degreesToRadians)
            let degree: Float = (atan2(sin(tLng - fLng) * cos(tLat), cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(tLng - fLng))).radiansToDegrees
            if degree >= 0 {
                return degree
            }
            else {
                return 360 + degree
            }
        }
    

    Objective C 代码:Move GMSMarker on Google Map Like UBER

    对于github:ARCarMovement

    【讨论】:

    • 嗨安东尼,感谢您的回答。如果你有一些 github 存储库会更好,无论是在 swift 和 Objective c 中
    • @MichelleRoot,我一定会去的
    • 谢谢...一旦你这样做了就让我知道..我当然急需它,它也会对其他人有所帮助..
    • @Antony Raphel 请告诉你有没有完成 github 项目...这对我会有帮助。
    • @Jessicathompson let newCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(CLLocationDegrees(dict!["lat"] as! Float), CLLocationDegrees(dict!["long"] as! Float)) 在这里通过网络服务传递您的纬度和经度,如果存在方位值,则也传递该值,否则只传递值 0。
    猜你喜欢
    • 2017-01-15
    • 2015-01-22
    • 2013-10-16
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2015-12-14
    相关资源
    最近更新 更多