GMSMarker没有按照路线移动

时间:2018-11-27 11:26:03

标签: swift google-maps cllocationcoordinate2d

我使用下面的API得到了指导。

  

https://maps.googleapis.com/maps/api/directions/json?origin=XYZ&destination=XYZ&sensor=false&mode=driving&key=XYZ

我使用 mode=driving 从我的Current Location coordinatesDestination Location Coordinates绘制了多段线,绘制成功,现在我想将该标记从“当前位置”移动到“目的地”位置,为此,我尝试使用以下代码,但标记仅沿直线移动,不像其他导航应用程序那样移动。

问题

标记不会按照多段线移动,而是直接在MAP上移动。

想要实现

我想将标记作为折线移动,就像UBER和其他App一样。

我点击了此链接:Move GMSMarker on Google Map Like UBER

let driverMarker = GMSMarker()
driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
driverMarker.rotation = CLLocationDegrees(getHeadingForDirection(fromCoordinate: currentCordinate, toCoordinate: destinationCordinate))
//found bearing value by calculation when marker add
driverMarker.position = currentCordinate
//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 = destinationCordinate
//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 = CLLocationDegrees(getHeadingForDirection(fromCoordinate: currentCordinate, toCoordinate: destinationCordinate))
//found bearing value by calculation
CATransaction.commit()



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
    }
}

任何建议都可以帮助实现这一目标。

预先感谢

0 个答案:

没有答案