0

Marker Movement from source to destination如何正确沿着折线移动标记?

图像显示标记运动是仅在一个方向标记的直线。 我想沿标记方向移动我的标记沿多段线应该随着多段线方向改变。如何实现这一点。 我当前的代码是这样的:

类MapScreenVC:BaseVC {

var path = GMSMutablePath() 
var arrayCoordinates : [CLLocationCoordinate2D] = [] 
var destCoord = CLLocationCoordinate2D() 
var marker = GMSMarker() 
var mapView : GMSMapView? = nil 
override func viewDidLoad() { 
    super.viewDidLoad() 

    let camera = GMSCameraPosition.camera(withLatitude: 53.4545, longitude: -2.1811, zoom: 14) 
    mapView = GMSMapView.map(withFrame: .zero, camera: camera) 
    marker = GMSMarker() 
    marker.position = CLLocationCoordinate2DMake(53.4387, -2.1827) 
    marker.title = "Source" 
    marker.snippet = "Source" 
    marker.icon = UIImage(named: "car") 
    marker.map = mapView 



    let DestinationMarker = GMSMarker() 
    self.destCoord = CLLocationCoordinate2DMake(53.4645, -2.1873) 
    DestinationMarker.position = CLLocationCoordinate2DMake(53.4643, -2.1869) 
    DestinationMarker.title = "Destination" 
    DestinationMarker.snippet = "Destination" 
    DestinationMarker.icon = UIImage(named: "home") 
    DestinationMarker.map = mapView 

//折线

path.addLatitude(53.4395, longitude:-2.1834) 
    path.addLatitude(53.4403, longitude:-2.1854) 
    path.addLatitude(53.4414, longitude:-2.1852) 
    path.addLatitude(53.4428, longitude:-2.1832) 
    path.addLatitude(53.4442, longitude:-2.1818) 
    path.addLatitude(53.4449, longitude:-2.1801) 
    path.addLatitude(53.4478, longitude:-2.1793) 
    path.addLatitude(53.4504, longitude:-2.1798) 
    path.addLatitude(53.4526, longitude:-2.1806) 
    path.addLatitude(53.4545, longitude:-2.1811) 
    path.addLatitude(53.4564, longitude:-2.1811) 
    path.addLatitude(53.4584, longitude:-2.1811) 
    path.addLatitude(53.4601, longitude:-2.1811) 
    path.addLatitude(53.4617, longitude:-2.1821) 
    path.addLatitude(53.4630, longitude:-2.1829) 
    path.addLatitude(53.4632, longitude:-2.1851) 
    path.addLatitude(53.4635, longitude:-2.1869) 
    path.addLatitude(53.4638, longitude:-2.1882) 
    path.addLatitude(53.4645, longitude:-2.1873) 


    let polyline = GMSPolyline(path: path) 
    polyline.strokeColor = .blue 
    polyline.strokeWidth = 6.0 
    polyline.geodesic = true 
    polyline.map = mapView 
    updateMarker(coordinates: destCoord) 
    view = mapView 
} 

func updateMarker(coordinates: CLLocationCoordinate2D) { 
    CATransaction.begin() 
    CATransaction.setAnimationDuration(10.0) 
    marker.position = coordinates 
    CATransaction.commit() 
} 

}

+0

如何u实现这一目标?请帮助我,如何处理? –

回答

0

我不熟悉的图书馆或类似的可给你这个,所以我会概述一个你可以用来做这个的手动方法。

要在多段线上移动标记,需要计算标记需要从每个点采取的步骤才能到达下一个点。
一种选择是做定义折线中的每个单独的行:在
1)定义线f(x) = ax + b为每题2分 如果你需要在计算直线的斜率帮助,您可以进一步阅读herea上面的公式)

2)定义标记所需的步骤,直到到达该行的末尾。你可以将线的长度除以常数,这会给你一个不变的step。这不是理想的,因为它会采取同样的步数跨短期和长期线移动,但它的一个开端。另外请注意,2个经纬度点之间的距离在一个球体上,并不像2维那样简单。有关更多信息,请参阅https://en.wikipedia.org/wiki/Haversine_formula

3)将在所述第一点(x1,y1)标记,并将其移动到(x1+step, f(x1+step))
您需要确定是否在线上向左或向右移动。

4)以后每次移动标记,检查它是否达到了当前行的终点,然后再重新开始下一行。