如何在添加坐标之前检查 GMSMutablePath 中是否存在坐标

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

我正在尝试在对象移动时在 Google 地图上绘制折线,有时发送的坐标可能会重复。我想防止将重复的坐标添加到 GMSMutablePath。反正这能实现吗?

目前我使用以下方法将坐标添加到GMSMutablePath。它还会添加重复值!

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

GoogleMaps SDK 中进行了一些挖掘之后,我得出了这个解决方案。它可能不是完美的,但你可以试一试。

可以使用GMSMutablePath

coordinate(at:index)方法遍历路径的所有坐标

迭代 GMSMutablePath 坐标。

//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)
}