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)以後每次移動標記,檢查它是否達到了當前行的終點,然後再重新開始下一行。