【问题标题】:How to clear polyline as the user walks through the road (polyline)当用户穿过道路时如何清除折线(折线)
【发布时间】:2020-06-07 20:39:18
【问题描述】:

我是地图技术的新手。 我正在使用 google apis 到 json 数据以使用折线获取地图上的路线。 我需要一些关于 polyline 的帮助。我想根据用户驾驶路线清除折线的路径。我尝试搜索许多解决方案,但无法为我工作。 我附上了相同的图片...

绘制折线的功能---

 func drawPath(from polyStr: String){
        print("inside Drawpath")
        path = GMSPath(fromEncodedPath: polyStr)!
        let polyline = GMSPolyline(path: path)

        polyline.strokeWidth = 6
        polyline.map = myMapView // Google MapView
        polyline.strokeColor = UIColor(red: 0, green: 128/255, blue: 1, alpha: 1)

        //   let camera = GMSCameraUpdate.fit(GMSCoordinateBounds(coordinate: CLLocationCoordinate2D(latitude: Double(lat), longitude: Double(long)), coordinate: CLLocationCoordinate2D(latitude: Double(directionlat)!, longitude: Double(directionlat)!)))
        let cameraUpdate = GMSCameraUpdate.fit(GMSCoordinateBounds(coordinate: CLLocationCoordinate2D(latitude: Double(lat), longitude: Double(long)), coordinate: CLLocationCoordinate2D(latitude: Double(directionlat)!, longitude: Double(directionlat)!)))


    //    self.timer = Timer.scheduledTimer(timeInterval: 0.003, target: self, selector: #selector(animatePolylinePath), userInfo: nil, repeats: true)


        //myMapView.moveCamera(cameraUpdate)
        let currentZoom = myMapView.camera.zoom
      //  myMapView.animate(toZoom: currentZoom - 1.4)
    }

感谢任何帮助。 谢谢和问候

【问题讨论】:

  • 您可以保存最后一个位置并使用 didUpdateLocations 位置监视新位置并获取用户最后一个位置,然后将其与保存的位置进行比较,然后如果它改变了绘制新的折线
  • 谷歌地图有没有类似 didupdatelocation 的功能?
  • 是的,更新位置是谷歌地图功能之一
  • 也许它对你有用 -> stackoverflow.com/a/44471050/7512091

标签: swift xcode polyline gmsmapview


【解决方案1】:

我遇到了同样的问题,我找到了一个在我当前的 2 个项目中运行良好的解决方案。所以在这里分享一下我的想法。 首先,从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
        }

不只是做魔术 注意:我正在维护一个变量来存储驾驶员在折线处的位置driversPositiionAtPath

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
    }

检查司机是否在路上,一旦他移动或每 10 秒。 快乐编码!

【讨论】:

  • 但是如果司机移动,如何获得位置或距离? @albinmrngstar
  • 您可以从设备 CLLocation Delegates 获取位置,并且 CLLocation 对象有一个名为 distance(from : CLLocation) 的函数,它返回距离两个 cllocation 点的距离(以米为单位)。检查此链接developer.apple.com/documentation/corelocation/cllocation
  • ``` if isPathChanged(byDriver : driversLocation){ self.wsToFetchNewPathFromDirection() }else{ self.drawRoute(for : polyPath ) } ``` 你在哪里叫这个??这是什么方法? wsToFetchNewPathFromDirection()
  • from wsToFetchNewPathFromDirection 是 Web 服务从 google Direction API 获取新路径的方法。你不是从谷歌方向 API 获得多边形字符串吗?关于 isPathChanged 在位置管理器委托中收到新位置时使用它(func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]))或每 10 秒一次。
  • 如果你不想面对以上所有的烦恼。只需每 10 秒用起点和终点点击一次方向 API -> 从地图上清除现有路径 -> 绘制新路径 [注意:每 1000 次点击谷歌收费 5 美元]
猜你喜欢
  • 2019-12-09
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
相关资源
最近更新 更多