xcode Swift:如何在 2 个坐标之间平滑移动 GMSMarker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37805398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:53:24  来源:igfitidea点击:

Swift: How to smoothly move GMSMarker between 2 coordinaets

iosswiftxcodegoogle-maps

提问by Ganesh Kumar

I have added a route and i have also added the way point markers. i want to move the marker smoothly from one gps coordinate point to another smoothly along the route. can anyone help me with this? here is the code that is used to add markers.

我添加了一条路线,我还添加了航点标记。我想沿着路线将标记从一个 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
    }
  }
}

回答by Sahil Kapoor

Swift:

迅速:

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

回答by dev_m

Check below code ...

检查下面的代码...

[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];

loop through all points you getting for path ..and set next destCoordinate as each next point ....

循环遍历您为 path 获得的所有点 .. 并将下一个 destCoordinate 设置为每个下一个点 ....

回答by user3672430

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