Search code examples
iosswiftgoogle-mapsgoogle-maps-api-3google-maps-sdk-ios

GMSMutablePath remove/replace coordinate issue, polyline


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.

The markers on this map are the coordinates of the polyline that Google Directions gave

enter image description here

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.

Issue: 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.

Here is my locationManager function

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 < 6) {
        //If not on last step
        if (pathIndex < Int(path.count()) - 1) {
            pathIndex++
            //Remove last path
            print("Removing: ", path.coordinateAtIndex(UInt(0)))
            path.removeCoordinateAtIndex(UInt(0))

        }
    }
}

I have the feeling it is skipping deleting because of the 0s in

path.removeCoordinateAtIndex(Uint(0))
path.replaceCoordinateAtIndex(UInt(0), withCoordinate: ...)

But after changing it around, I'm still getting the same issue, so I think it might be another thing.

This is the debug log. 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):

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

What is it doing that is making it skip the second waypoint/coordinate?

PS: 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?


Solution

  • Found the problem!

    The line

    let waypoint = CLLocation(latitude: path.coordinateAtIndex(UInt(pathIndex)).latitude, longitude: path.coordinateAtIndex(UInt(pathIndex)).longitude)
    

    Should be reading:

    let waypoint = CLLocation(latitude: path.coordinateAtIndex(UInt(1)).latitude, longitude: path.coordinateAtIndex(UInt(1)).longitude)
    

    Instead. Notice the change from pathIndex -> 1, so now distanceFromLocation will always be comparing just to the next coordinate in path.