如何在ios中将谷歌地图标记从一个位置平滑地动画到另一个位置

如何在ios中将谷歌地图标记从一个位置平滑地动画到另一个位置,ios,swift,google-maps,navigation,marker,Ios,Swift,Google Maps,Navigation,Marker,我在谷歌地图上有一个汽车图标,它必须在固定的时间间隔内移动。位置坐标将从服务器获取,我已经通过执行下面的代码成功地更改了标记的位置,但Didit获得了平滑的移动 let camera = GMSCameraPosition.camera(withLatitude: (((self.driverArrival.value(forKey: "latitude")) as AnyObject).doubleValue)!,

我在谷歌地图上有一个汽车图标,它必须在固定的时间间隔内移动。位置坐标将从服务器获取,我已经通过执行下面的代码成功地更改了标记的位置,但Didit获得了平滑的移动

let camera = GMSCameraPosition.camera(withLatitude: (((self.driverArrival.value(forKey: "latitude")) as AnyObject).doubleValue)!,
                                          longitude: (((self.driverArrival.value(forKey: "longitude")) as AnyObject).doubleValue)!, zoom: 15)

    let position = CLLocationCoordinate2DMake((((self.driverArrival.value(forKey: "latitude")) as AnyObject).doubleValue)!, (((self.driverArrival.value(forKey: "longitude")) as AnyObject).doubleValue)!)

    driverMarker.position = position
    driverMarker.map = self.mapView
此处driverdetails包含所有必需的数据。只是想知道我可以使用任何动画功能来实现这一点

可以使用animate(to:GMSCameraPosition)使用动画更新贴图位置示例如下:-

func updateMapLocation(lattitude:CLLocationDegrees,longitude:CLLocationDegrees){
    let camera = GMSCameraPosition.camera(withLatitude: lattitude, longitude: longitude, zoom: 16)
    mapView?.camera = camera
    mapView?.animate(to: camera)
}
然后像这样调用该方法

updateMapLocation(lattitude:-33.8683,longitude:151.2086)

编辑

对于标记位置更新,可以使用单个标记并使用此代码更新其位置

CATransaction.begin()
CATransaction.setAnimationDuration(2.0)
marker.position = coordindates // CLLocationCoordinate2D coordinate
CATransaction.commit()
可以使用animate(to:GMSCameraPosition)使用动画更新贴图位置示例如下:-

func updateMapLocation(lattitude:CLLocationDegrees,longitude:CLLocationDegrees){
    let camera = GMSCameraPosition.camera(withLatitude: lattitude, longitude: longitude, zoom: 16)
    mapView?.camera = camera
    mapView?.animate(to: camera)
}
然后像这样调用该方法

updateMapLocation(lattitude:-33.8683,longitude:151.2086)

编辑

对于标记位置更新,可以使用单个标记并使用此代码更新其位置

CATransaction.begin()
CATransaction.setAnimationDuration(2.0)
marker.position = coordindates // CLLocationCoordinate2D coordinate
CATransaction.commit()

请不要使用GMSCAMERAPOSION在地图中移动pin

self.mapView.animate(toLocation: CLLocationCoordinate2D(latitude: YOUR_LATITUDE, longitude: YOUR_LONGITUDE))
self.marker.position = coordinate
self.marker.map = self.mapView
您可以使用mapView.animate(toLocation:YOUR_坐标)方法在地图中平滑移动pin

self.mapView.animate(toLocation: CLLocationCoordinate2D(latitude: YOUR_LATITUDE, longitude: YOUR_LONGITUDE))
self.marker.position = coordinate
self.marker.map = self.mapView

希望这有帮助

请不要在地图中使用GMSCAMERAPOSION作为移动pin

self.mapView.animate(toLocation: CLLocationCoordinate2D(latitude: YOUR_LATITUDE, longitude: YOUR_LONGITUDE))
self.marker.position = coordinate
self.marker.map = self.mapView
您可以使用mapView.animate(toLocation:YOUR_坐标)方法在地图中平滑移动pin

self.mapView.animate(toLocation: CLLocationCoordinate2D(latitude: YOUR_LATITUDE, longitude: YOUR_LONGITUDE))
self.marker.position = coordinate
self.marker.map = self.mapView
希望这有帮助

试试这个

将GMSMapViewDelegate添加到类中

self.mapView.delegate=self//Call delegate

//MARK - MapView delegates
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    self.marker.map = mapView;
    self.marker.position = position.target //Your target position
    self.mapView.selectedMarker = self.marker //Your marker
    DispatchQueue.main.async {
        self.marker.snippet = "Getting address... " //Your snippet title 
    }
}

func mapView(_ mapView:GMSMapView, idleAt position:GMSCameraPosition) {
    //Get address with village names and street names
    self.getAddressForLatLng(latitude: "\(position.target.latitude)", longitude: "\(position.target.longitude)", zoomLevel: position.zoom)
}
试试这个

将GMSMapViewDelegate添加到类中

self.mapView.delegate=self//Call delegate

//MARK - MapView delegates
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    self.marker.map = mapView;
    self.marker.position = position.target //Your target position
    self.mapView.selectedMarker = self.marker //Your marker
    DispatchQueue.main.async {
        self.marker.snippet = "Getting address... " //Your snippet title 
    }
}

func mapView(_ mapView:GMSMapView, idleAt position:GMSCameraPosition) {
    //Get address with village names and street names
    self.getAddressForLatLng(latitude: "\(position.target.latitude)", longitude: "\(position.target.longitude)", zoomLevel: position.zoom)
}

我已经这样做了,但这不是我想要的,我想在地图中设置标记的动画,而不是相机的位置。您是否每次获得新坐标时都创建标记?在上面的代码中,我所做的就像每次清除地图视图并创建新标记一样。但是现在第一次将标记添加到mapview,然后仅更改位置Yes检查标记==nil,然后初始化它,然后仅更新其位置,而不是每次创建标记,在你们班上宣布标记。@MidhunNarayan至少有礼貌地回答其他人在这里投入了宝贵的时间。我这样做了,但这不是我想要的,我想在地图上设置标记的动画,而不是相机的位置。你是否每次获得新坐标时都创建标记?在上面的代码中,我所做的就像每次清除地图视图并创建新标记一样。但是现在第一次将标记添加到mapview,然后仅更改位置Yes检查标记==nil,然后初始化它,然后仅更新其位置,而不是每次创建标记,在你的课堂上声明标记。@MidhunNarayan至少有礼貌地回答其他人在这里投入了宝贵的时间。在这种情况下,标记的移动将是平稳的?是的,你确定可以尝试并检查??请参阅本文档[在这种情况下,标记器的移动将是平稳的?是的,请确保您可以尝试并检查??请参阅本文档[