Swift:如何在两个协调之间平稳地移动GMSMarker

时间:2016-06-14 07:18:04

标签: ios swift xcode google-maps

我添加了一条路线,我还添加了点标记的方式。我想沿着路线平稳地将标记从一个GPS坐标点移动到另一个平滑点。谁能帮我这个?这是用于添加标记的代码。

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

3 个答案:

答案 0 :(得分:5)

夫特:

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

答案 1 :(得分:4)

检查以下代码......

[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 :(得分:0)

var driverPositionMarker: GMSMarker?

func displayDriverPosition(on mapView: GMSMapView, with coordinate: CLLocationCoordinate2D) {
        if let marker = driverPositionMarker {
            let heading = GMSGeometryHeading(marker.position, coordinate)
            marker.rotation = heading
            marker.position = coordinate
        } else {
            let marker = GMSMarker()
            marker.position = coordinate
            marker.iconView = UIImageView(image: #imageLiteral(resourceName: "CarMarker"))
            marker.map = mapView
            driverPositionMarker = marker
        }
}