2016-06-14 62 views
1

我已經添加了一個路線,並且我還添加了點標記的方式。我想沿着路線順利地將標記從一個GPS座標點移動到另一個座標點。誰能幫我這個?這裏是用來添加標記的代碼。Swift:如何平穩移動GMSMarker在2個座標之間

func configureMapAndMarkersForRoute() { 
    viewGMap.camera = GMSCameraPosition.cameraWithTarget(mapTasks.originCoordinate, zoom: 9.0) 

    originMarker = GMSMarker(position: self.mapTasks.originCoordinate) 
    originMarker.map = self.viewGMap 
    originMarker.icon = GMSMarker.markerImageWithColor(UIColor.greenColor()) 
    originMarker.title = self.mapTasks.originAddress 

    destinationMarker = GMSMarker(position: self.mapTasks.destinationCoordinate) 
    destinationMarker.map = self.viewGMap 
    destinationMarker.icon = GMSMarker.markerImageWithColor(UIColor.redColor()) 
    destinationMarker.title = self.mapTasks.destinationAddress 

    if waypointsArray.count > 0 { 
    var i = 0 
    for waypoint in waypointsArray { 
     let lat: Double = (waypoint.componentsSeparatedByString(",")[0] as NSString).doubleValue 
     let lng: Double = (waypoint.componentsSeparatedByString(",")[1] as NSString).doubleValue 

     let marker = GMSMarker(position: CLLocationCoordinate2DMake(lat, lng)) 
     marker.map = viewGMap 
     marker.icon = GMSMarker.markerImageWithColor(UIColor.purpleColor()) 
     marker.title = locationNameArray[i] 
     markersArray.append(marker) 
     i += 1 
    } 
    } 
} 
+0

爲什麼每次都添加標記?只是沿着路徑移動單個標記... –

+0

@ kishan94是的。這就是我想要做的。你能幫助我嗎? –

+0

如果您發現我的解決方案正確,請將答案標記爲正確。 –

回答

2

檢查下面的代碼...

[CATransaction begin]; 
[CATransaction setAnimationDuration:5.0]; 
CGPoint point = [mapView.projection pointForCoordinate:destCoordinate]; 
// point.x = point.x + 100; 
GMSCameraUpdate *camera = 
[GMSCameraUpdate setTarget:[mapView.projection coordinateForPoint:point]]; 
[mapView animateWithCameraUpdate:camera]; 
markerToMove.position = destCoordinate; 
[CATransaction commit]; 

遍歷您是否獲得了路徑中的所有點..和設置下一個destCoordinate爲每一個點....

2

斯威夫特:

func updateMarker(coordinates: CLLocationCoordinate2D, degrees: CLLocationDegrees, duration: Double) { 
    // Keep Rotation Short 
    CATransaction.begin() 
    CATransaction.setAnimationDuration(0.5) 
    marker.rotation = degrees 
    CATransaction.commit() 

    // Movement 
    CATransaction.begin() 
    CATransaction.setAnimationDuration(duration) 
    marker.position = coordinates 

    // Center Map View 
    let camera = GMSCameraUpdate.setTarget(coordinates) 
    mapView.animateWithCameraUpdate(camera) 

    CATransaction.commit() 
} 
+0

謝謝。在迅速3也,它的工作。 –

+0

我有一點疑問。我想要根據服務器的緯度和經度旋轉標記器 –

+1

@UmaMadhavi Here:http://stackoverflow.com/a/41197689/3950397 –