Search code examples
iosswiftgoogle-maps-sdk-ios

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
    }
}

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)
        }
    }
}

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       
  }
}

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?


Solution

  • 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
         ...    
    }