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



我试图在物体行进时在谷歌地图上画一条折线,有时发送的坐标可能会重复。我想防止重复的坐标被添加到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)
}

最新更新