简体   繁体   English

如何通过经度和纬度值更新Google地图上的标记位置

[英]How to update the marker position on google map by latitude and longitude values

I am new to iOS Swift. 我是iOS Swift的新手。 I recently started working on GMSMaps . 我最近开始研究GMSMaps In my code I am able to get the latitude and longitude values of source and destination to find the direction and I am also able to add the marker to map. 在我的代码中,我可以获取源和目标的latitudelongitude值以找到方向,也可以在地图上添加标记。 But, I want to change the position of marker based on changing the source and destination values. 但是,我想基于更改源和目​​标值来更改标记的位置。 When I tried to change source/destination ,then again one more marker is adding to maps. 当我尝试更改源/目标时,再次在地图上添加了一个标记。 I don't want to add one more marker, I just want to move the previous marker to updated location . 我不想再添加一个标记,我只想将之前的标记移到更新的位置。 If any one gives solution it would be so great. 如果有人提供解决方案,那就太好了。

This is my code which I have written to add marker for source and destination. 这是我编写的用于添加源和目标标记的代码。 I have written this code in didSelectRowAtIndexPath method of tableView 我已经在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

Make the marker as global variable and update its coordinates via position property. marker设为全局变量,并通过position属性更新其坐标。

var marker: GMSMarker!

Update marker: 更新标记:

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 https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_marker#a2b9fdae0160d7acf439889ffcdb5f68b

in tableViewDidSelect 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM