Swift 如何在用户穿过道路时清除多段线(多段线)
我不熟悉地图技术。 我正在使用GoogleAPI访问json数据,以使用多段线获取地图上的路线。 我需要一些关于多段线的帮助。我想清除多段线的路径,根据用户在路线中的行驶情况。我试图寻找许多解决办法,但都不管用。 我已经附上了相同的图像。。。 函数来绘制多段线--- 感谢您的帮助。Swift 如何在用户穿过道路时清除多段线(多段线),swift,xcode,polyline,gmsmapview,Swift,Xcode,Polyline,Gmsmapview,我不熟悉地图技术。 我正在使用GoogleAPI访问json数据,以使用多段线获取地图上的路线。 我需要一些关于多段线的帮助。我想清除多段线的路径,根据用户在路线中的行驶情况。我试图寻找许多解决办法,但都不管用。 我已经附上了相同的图像。。。 函数来绘制多段线--- 感谢您的帮助。 感谢和问候我也遇到了同样的问题,我找到了一个解决方案,在我目前的两个项目中效果很好。让我在这里分享我的想法。 首先,从direction API获取初始的polypathString。现在,您可以通过使用GMSPa
感谢和问候我也遇到了同样的问题,我找到了一个解决方案,在我目前的两个项目中效果很好。让我在这里分享我的想法。 首先,从direction API获取初始的polypathString。现在,您可以通过使用GMSPath对象并将其与CLLocation.distance方法相结合来获得路径中的点数。我已尝试查找驱动程序是否在路径中
If he is found on the path
I take the index from array in which the driver is close and start drawing from there
else
I request to fetch an another direction API since he is not the path drawing it is not right.
let polyPath = GMSPath.init(fromEncodedPath: pathStr)!
func isPathChanged(byDriver coordinate : CLLocation) -> Bool{
guard self.path.count() != 0 else{return true}
for range in 0..<path.count(){
let point = path.coordinate(at: range).location//CLLocation
if point.distance(from: coordinate) < 75{
self.driversPositiionAtPath = range
return false
}
}
self.driversPositiionAtPath = 0
return true
}
如果在路径上找到他
我从驱动程序靠近的数组中获取索引,并从那里开始绘制
其他的
我请求获取另一个方向的API,因为他不是绘制路径的人,这是不对的。
让polyPath=GMSPath.init(fromEncodedPath:pathStr)!
func isPathChanged(按驱动程序坐标:CLLocation)->Bool{
guard self.path.count()!=0 else{return true}
对于0中的范围。您可以保存最后一个位置并使用didUpdateLocations位置监视新位置,获取用户的最后一个位置,然后将其与保存的位置进行比较,如果更改,则绘制新的多段线是否有类似于谷歌地图的didupdatelocation的功能?是的,didupdatelocation是谷歌地图功能之一,可能适用于您->但是如果驱动程序移动,如何获取位置或距离???@albinmrngstar您可以从设备CLLocation代理获取位置,并且CLLocation对象有一个名为distance(from:CLLocation)的函数,该函数返回两个CLLocation点的距离(以米为单位)。如果iPathChanged(byDriver:driversLocation),请检查此链接{self.wsToFetchNewPathFromDirection()}else{self.drawRoute(for:polyPath)}``您在哪里调用了此方法?此方法是什么?wsToFetchNewPathFromDirection()from wsToFetchNewPathFromDirection是web服务从google Direction API获取新路径的方法。您不是从google Direction API获取多边形字符串吗。关于isPathChanged在收到location manager委托中的新位置时使用它(func location manager(u manager:CLLocationManager,didUpdateLocations locations:[CLLocation]))或每10秒一次。如果你不想面对上述所有问题,只需使用起点和终点每10秒点击一次方向API即可->从地图中清除现有路径->绘制新路径[注意:每点击1000次,谷歌收费5美元]
If he is found on the path
I take the index from array in which the driver is close and start drawing from there
else
I request to fetch an another direction API since he is not the path drawing it is not right.
let polyPath = GMSPath.init(fromEncodedPath: pathStr)!
func isPathChanged(byDriver coordinate : CLLocation) -> Bool{
guard self.path.count() != 0 else{return true}
for range in 0..<path.count(){
let point = path.coordinate(at: range).location//CLLocation
if point.distance(from: coordinate) < 75{
self.driversPositiionAtPath = range
return false
}
}
self.driversPositiionAtPath = 0
return true
}
if isPathChanged(byDriver : driversLocation){
self.wsToFetchNewPathFromDirection()
}else{
self.drawRoute(for : polyPath )
}
func drawRoute(for path : GMSPath){
let drawingPath = GMSMutablePath()
for i in self.driversPositiionAtPath..<path.count(){
drawingPath.add(path.coordinate(at: i))
}
self.polyline.path = drawingPath
self.polyline.strokeColor = UIColor.black
self.polyline.strokeWidth = 3.0
self.polyline.map = mapLocation
}