我试图随着对象的移动在Google地图上绘制一条折线,有时发送的坐标可能会重复。我想防止将重复坐标添加到GMSMutablePath中。无论如何要实现?

当前,我使用以下方法将坐标添加到GMSMutablePath中。它也添加重复的值!

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

最佳答案

在对GoogleMaps SDK进行一些挖掘之后,我得出了这个解决方案。它可能不是完美的选择,但您可以尝试一下。

您可以使用coordinate(at:index)GMSMutablePath方法遍历路径的所有坐标

迭代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)
}

关于ios - 添加坐标之前如何检查GMSMutablePath中是否存在坐标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45141344/

10-09 10:17