title: How to update Google Maps marker position in swift/iOS
body: <p>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):</p>
  
  <pre><code>func showMarkerOnMap() {
      mapView.clear() //&lt;- 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
      }
  }
  </code></pre>
  
  <p>This is how I get <code>dataFromServer</code> using Alamofire:</p>
  
  <pre><code>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 &lt; 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)
          }
      }
  }
  </code></pre>
  
  <p>Basically I just append all received data to my <code>dataFromServer</code> which belongs to <code>dataClass</code> class:</p>
  
  <pre><code>class dataClass: NSObject {
  var latitude: Double
  var longitude: Double
  
  init(latitude: Double, longitude: Double) {
      self.latitude = latitude
      self.longitude = longitude       
    }
  }
  </code></pre>
  
  <p>My <code>getCoordinatesFromServer()</code> function is being called every 3 seconds (for now). What I was expecting to receive new coordinates (and I do receive them for sure), then<code>showMarkerOnMap()</code> 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.</p>
  
  <p>Any suggestion on how to improve my code or some alternative?</p>