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
}
}
This is how I get dataFromServer
using Alamofire:
这就是我使用Alamofire获取dataFromServer的方法:
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)
}
}
}
Basically I just append all received data to my dataFromServer
which belongs to dataClass
class:
基本上我只是将所有收到的数据附加到属于dataClass类的dataFromServer:
class dataClass: NSObject {
var latitude: Double
var longitude: Double
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
}
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.
我的getCoordinatesFromServer()函数每3秒调用一次(现在)。我期待接收新坐标(我确实收到它们),应该调用thenshowMarkerOnMap(),从而清除所有现有标记并创建新闻。我得到了什么 - 标记重复和显着滞后。如果转到另一个视图然后返回到包含mapView的View,原始标记将消失。
Any suggestion on how to improve my code or some alternative?
关于如何改进我的代码或其他替代方案的任何建议?
1 个解决方案
#1
1
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
...
}
#1
1
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
...
}