title: GMSMutablePath remove/replace coordinate issue, polyline
body: <p>I'm building an app that uses the Google Maps Directions API to give me a route polyline so I can display on my mapView to the users. Kind of like the Google Maps turn-by-turn service, but inside my app.</p>
  
  <p>The markers on this map are the coordinates of the polyline that Google Directions gave</p>
  
  <p><a href="https://i.stack.imgur.com/QR2te.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QR2te.png" alt="enter image description here"></a></p>
  
  <p>All the above coordinates (the markers) are stored in variable "path", which is a GMSMutablePath. Whenever the user's location is near a coordinate on the path (traveling in order so coordinate 0 first, then 1..) I remove that coordinate from the path via removeCoordinateAtIndex(), so that the polyline disappears (for ex: from marker 0 to marker 1) from my map. The purpose is that I want the polyline to always start displaying from the user's location, and not the origin of my route.</p>
  
  <p><strong>Issue:</strong>
  However, I'm not sure why but the if statement that leads to removeCoordinateAtIndex() seems to only delete when I am on the index 0.. 2.. 4 (even) on path (my GMSMutablePath), so skipping one in between.</p>
  
  <p>Here is my locationManager function</p>
  
  <pre><code>pathIndex = 0
  
  func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      let location = locations[locations.count - 1]
  
      /*
          For each GPS update, check location of user against next waypoint 
      on route. If distance is within 6 meters (18 feet), increment pathIndex 
      and now draw polyline from current location to NEXT waypoint (if there  
      is one), and then compare user location to NEXT waypoint again, etc.
      */
  
      //Replace polyline to start display from where you are
      path.replaceCoordinateAtIndex(UInt(0), withCoordinate: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude))
      polyline.path = path
  
      //Get distance from current to next waypoint in path
      let waypoint = CLLocation(latitude: path.coordinateAtIndex(UInt(pathIndex)).latitude, longitude: path.coordinateAtIndex(UInt(pathIndex)).longitude) //Coordinate of next waypoint
      let locToWaypoint = location.distanceFromLocation(waypoint) //Returns distance in meters
      print("dist: ", locToWaypoint, ", index: ", pathIndex)
  
      //If closer than 6 meters, remove polyline of that segment from map
      if (locToWaypoint &lt; 6) {
          //If not on last step
          if (pathIndex &lt; Int(path.count()) - 1) {
              pathIndex++
              //Remove last path
              print("Removing: ", path.coordinateAtIndex(UInt(0)))
              path.removeCoordinateAtIndex(UInt(0))
  
          }
      }
  }
  </code></pre>
  
  <p>I have the feeling it is skipping deleting because of the 0s in   </p>
  
  <pre><code>path.removeCoordinateAtIndex(Uint(0))
  path.replaceCoordinateAtIndex(UInt(0), withCoordinate: ...)
  </code></pre>
  
  <p>But after changing it around, I'm still getting the same issue, so I think it might be another thing. </p>
  
  <p>This is the <strong>debug log</strong>. Notice that only the first and third call removes the coordinate, the second call down. This changes to second and fourth call when I initiate pathIndex to 1 instead (always skipping one in between):</p>
  
  <pre><code>dist: 0.0 , index:  0       
  Removing:  CLLocationCoordinate2D(latitude: 36.131648, longitude: -80.275542)  //First time, so is removed. This is the origin.
  dist: 40.3950268964149 , index:  1     //Now showing dist to next waypoint
  
  dist: 14.8659227375514 , index:  1  //Second time; NOT removed. Somehow it just skips the coordinate at the index and goes on to calculate the distance to the NEXT waypoint (thus 14.86)
  
  dist: 1.34445248711346 , index:  1  
  Removing:  CLLocationCoordinate2D(latitude: 36.131931, longitude: -80.2758)   //Third time, IS removed.
  dist: 45.4634994640154 , index:  2     //Now showing dist to next waypoint
  </code></pre>
  
  <p>What is it doing that is making it skip the second waypoint/coordinate?</p>
  
  <p><strong>PS:</strong> When I call removeCoordinateAtIndex(0), does everything slide back down one spot? For example, if the path before remove is: [0, 1, 2, 3], will the path after remove be: [1, 2, 3] (so resized), or actually [ , 1, 2, 3] with a blank at index 0?</p>