如何在Swift/iOS中更新Google Maps标记位置 [英] How to update Google Maps marker position in swift/iOS
问题描述
我希望有多个标记,并根据服务器上的新数据更新其位置.这是我在地图上显示标记(基本内容)的方式:
I would like to have multiple markers and update their position, based on new data from server. This is how I show marker on map (basic stuff):
func showMarkerOnMap() {
mapView.clear() //<- That's GMSMapView
for all in dataFromServer {
let lat5 = all.latitude
let lon5 = all.longitude
let position = CLLocationCoordinate2DMake(lat5, lon5)
let marker = GMSMarker(position: position)
marker.flat = true
marker.map = self.mapView
}
}
这就是我使用Alamofire获得dataFromServer
的方式:
This is how I get dataFromServer
using Alamofire:
var dataFromServer = [dataClass]()
func getCoordinatesFromServer(){
//Here goes headers and authentication data
Alamofire.request(.GET, URL, headers: headers).authenticate(user: oranges, password: appels).responseJSON { response in
switch response.result {
case .Success:
//Remove existing dataFromServer
self.dataFromServer.removeAll()
if let value = response.result.value {
let json = JSON(value)
for result in json.arrayValue {
let lat = result["latitude"].doubleValue
let lon = result["longitude"].doubleValue
let zip = dataClass(latitude: lat, longitude: lon)
//The next part is for checking that coordinates do not overlap
if self.dataFromServer.count < 1 {
self.dataFromServer.append(zip)
} else {
for all in self.dataFromServer {
guard all.latitude != lat else {
return
}
self.trblInfo1.append(zip)
}
}
}
//This should update existing markers?
self.showMarkerOnMap()
}
case .Failure(let error):
print(error)
}
}
}
基本上,我只是将接收到的所有数据追加到属于dataClass
类的dataFromServer
中:
Basically I just append all received data to my dataFromServer
which belongs to dataClass
class:
class dataClass: NSObject {
var latitude: Double
var longitude: Double
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
}
我的getCoordinatesFromServer()
函数每3秒被调用一次(现在).我原本希望收到新坐标的(并且我确实会收到它们),然后应调用showMarkerOnMap()
,从而清除所有现有标记并创建新闻.我得到的是-标记重复和明显的滞后.如果转到另一个View,然后返回到包含mapView的View,则原始标记会消失.
My getCoordinatesFromServer()
function is being called every 3 seconds (for now). What I was expecting to receive new coordinates (and I do receive them for sure), thenshowMarkerOnMap()
should be called thus clearing all existing markers and creating news. What I get - marker duplicate and noticeable lag. The original marker disappears if go to another View and then comeback to View containing mapView.
关于如何改进我的代码或其他替代方法的任何建议?
Any suggestion on how to improve my code or some alternative?
推荐答案
如果您有来自服务器的职位的唯一标识符,则可以保留一个标记列表,然后在到达新数据时更新它们的位置.像这样:
If you have any kind of unique identifier for your positions that came from server, you can keep a list of markers and then update their location when new data arrive. Something like this:
for result in json.arrayValue {
let lat = result["latitude"].doubleValue
let lon = result["longitude"].doubleValue
let identifier = result["identifier"] as! String
self.myMarkersDictionary[identifier]?.position.latitude = lat
self.myMarkersDictionary[identifier]?.position.longitude = lon
...
}
这篇关于如何在Swift/iOS中更新Google Maps标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!