gpt4 book ai didi

ios - 如何通过纬度和经度值更新谷歌地图上的标记位置

转载 作者:行者123 更新时间:2023-11-30 10:55:24 27 4
gpt4 key购买 nike

我是 iOS Swift 新手。我最近开始研究 GMSMaps。在我的代码中,我能够获取源和目的地的纬度和经度值来查找方向,并且我还能够将标记添加到 map 。但是,我想根据更改源值和目标值来更改标记的位置。当我尝试更改源/目的地时,又一个标记添加到 map 中。我不想再添加一个标记,我只想将前一个标记移动到更新的位置。如果有人给出解决方案那就太好了。

这是我编写的代码,用于添加源和目标的标记。我已经在 tableView 的 didSelectRowAtIndexPath 方法中编写了这段代码

let centerr = CLLocationCoordinate2D(latitude: lat, longitude: lon)
var camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 10);
self.googlemapsview.camera = camera
var marker = GMSMarker(position: centerr)
print("Latitude :- \(lat)")
print("Longitude :-\(lon)")
marker.map = self.googlemapsview

最佳答案

marker设为全局变量,并通过position属性更新其坐标。

var marker: GMSMarker!

更新标记:

let newPosition = CLLocationCoordinate2D(latitude: newLat, longitude: newLon)
marker.position = newPosition

https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_marker#a2b9fdae0160d7acf439889ffcdb5f68b

tableViewDidSelect

let centerr = CLLocationCoordinate2D(latitude: lat, longitude: lon)
var camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 10);
self.googlemapsview.camera = camera
marker = GMSMarker(position: centerr)
print("Latitude :- \(lat)")
print("Longitude :-\(lon)")
marker.map = self.googlemapsview

关于ios - 如何通过纬度和经度值更新谷歌地图上的标记位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54019923/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com