2

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?

Xernox
  • 1,706
  • 1
  • 23
  • 37

1 Answers1

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
     ...    
}
Will Glück
  • 1,242
  • 12
  • 17
  • This sounds great and I do have a custom identifier, but I am not aware how to store markers in a dictionary? How to do that? – Xernox Jan 05 '16 at 08:01
  • Should I create markers dictionary like this: `var myMarkersDictionary = [String:GMSMarker]()`? – Xernox Jan 05 '16 at 08:46
  • Exactly like that. When you want to remove a marker you can just set the .map property to null. – Will Glück Jan 06 '16 at 09:55
  • @WillGlück.. here what is "identifier". I am getting only latitude & longitude in server – Uma Madhavi Dec 16 '16 at 07:25
  • It was just a example. To use this solution you need to have any way to identify the marker using the server data that you are receiving. With some kind of identifier you can update previous markers that are saved on the dictionary or create a new one. – Will Glück Dec 16 '16 at 17:59
  • @WillGlück.. When i user travelling how to set marker position in google maps can u guide me. – Uma Madhavi Mar 30 '17 at 05:51
  • Hi there. Plz ask another question, I will answer if I can, but many others will surely help you too. Take care o/ – Will Glück Apr 03 '17 at 12:42