Search code examples
iosiphonegoogle-mapsgoogle-maps-sdk-ios

How to check if a coordinate exists in GMSMutablePath before adding a coordinate


I'm trying to draw a polyline on the Google map as a object travels, sometimes the coordinates sent can get duplicated. I want to prevent duplicate coordinate from being added to the GMSMutablePath. Anyway this be achieved?

Currently I use the following method to add the coordinate to the GMSMutablePath. It adds duplicate values as well!

self.path.addLatitude(coordinate.latitude, longitude: coordinate.longitude)

Solution

  • After doing some digging in GoogleMaps SDK, I arrived at this solution. It may not be the perfect one but you can give it a try.

    You can iterate through all the coordinates of the path by using coordinate(at:index) method of GMSMutablePath

    Iterating the GMSMutablePath coordinates.

    //Here path is your GMSMutablePath
    for i in 0..<path.count() {
        let coordinate = path.coordinate(at: i)
        //The coordinate received is a CLLocationCoordinate2D type from which you can get the latitude and longitude.
    
        //Here check the coordinate latitude and longitude is same as your received coordinate, make a return else add to your path. 
        //You can also keep a flag variable and at the end of all iterations, you can check whether the coordinate is present or not. 
    
        print(coordinate)
    }