【问题标题】:Remove travelled path from GMSPolyline on GMSMapView Swift iOS从 GMSMapView Swift iOS 上的 GMSPolyline 中删除行进路径
【发布时间】:2018-07-29 15:44:35
【问题描述】:

我正在使用谷歌距离 api ["https://maps.googleapis.com/maps/api/directions/json?origin=" +start.latitude + "," + start.longitude +"&destination=" + end.latitude +"," + end.longitude + "&alternatives=false" +"&mode=driving&key=" + key;] 获取从起点到终点的路线。

我正在使用以下代码在起点和终点之间绘制路线

func drawPath()
{
    if polylines != nil {
        polylines?.map = nil
        polylines = nil
    }

    if animationPolyline != nil {
        self.animationIndex = 0
        self.animationPath = GMSMutablePath()
        self.animationPolyline.map = nil
        if self.timer != nil {
            self.timer.invalidate()
        }
    }


    setupStartRideLocationMarkup(CLLocationCoordinate2D(latitude: (currentLocation?.coordinate.latitude)!, longitude: (currentLocation?.coordinate.longitude)!))

    if currentLocation != nil && destinationLocation != nil {
        let origin = "\((currentLocation?.coordinate.latitude)!),\((currentLocation?.coordinate.longitude)!)"
        let destination = "\((destinationLocation?.latitude)!),\((destinationLocation?.longitude)!)"


        let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=MY_API_KEY"

        Alamofire.request(url).responseJSON { response in


            let json = JSON(data: response.data!)
            self.jsonRoute = json
            let routes = json["routes"].arrayValue

            for route in routes
            {
                let routeOverviewPolyline = route["overview_polyline"].dictionary
                let points = routeOverviewPolyline?["points"]?.stringValue
                self.path = GMSPath.init(fromEncodedPath: points!)!
                self.polylines = GMSPolyline.init(path: self.path)
                self.polylines?.geodesic = true
                self.polylines?.strokeWidth = 5
                self.polylines?.strokeColor = UIColor.black
                self.polylines?.map = self.mapView
            }

            self.shouldDrawPathToStartLocation()
            self.shouldDrawPathToEndLocation()

            if routes.count > 0 {
                self.startAnimatingMap()
            }
        }
    }
}

如您所见,我正在使用来自 api 的编码路径初始化路径。现在我想从整体路径中删除旅行的 GMSPolyline 我该怎么做?我目前的直觉是它将来自 didUpdateLocations 这是我的 didUpdateLocations 方法代码

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    currentLocation = locations.last!

    let camera = GMSCameraPosition.camera(withLatitude: (currentLocation?.coordinate.latitude)!,
                                          longitude: (currentLocation?.coordinate.longitude)!,
                                          zoom: zoomLevel)

    if (mapView?.isHidden)! {
        mapView?.isHidden = false
        mapView?.camera = camera
    } else {
        mapView?.animate(to: camera)
    }

    updatePolyLineIfRequired()

}

并且在 updatePolyLineIfRequired 我想删除旅行折线

func updatePolyLineIfRequired(){
    if GMSGeometryIsLocationOnPath((currentLocation?.coordinate)!, path, true) {
        if startPolyline != nil {
            startPolyline?.map = nil
            startPolyline = nil
        }

    }
}

我想实施像 Uber 或 Careem 这样的解决方案,其中移动绘制的 GMSPolyline 被删除,直到用户当前位置。

提前致谢 P.S 我正在使用 Alamofire SwiftyJSON

【问题讨论】:

    标签: ios swift swift4 gmsmapview google-directions-api


    【解决方案1】:

    有两种解决方案:-

    1. 每次调用 didUpdateLocations 函数时都会调用 Directions Api。(效率不高)
    2. 从 GMSPath 中移除行进坐标。

    除非您对 Direction api 的请求限制较少,否则调用 Directions api 将无用。

    用于从路径中删除行进坐标:-

        //Call this function in didUpdateLocations
    func updateTravelledPath(currentLoc: CLLocationCoordinate2D){
        var index = 0
        for i in 0..<self.path.count(){
            let pathLat = Double(self.path.coordinate(at: i).latitude).rounded(toPlaces: 3)
            let pathLong = Double(self.path.coordinate(at: i).longitude).rounded(toPlaces: 3)
    
            let currentLaenter code heret = Double(currentLoc.latitude).rounded(toPlaces: 3)
            let currentLong = Double(currentLoc.longitude).rounded(toPlaces: 3)
    
            if currentLat == pathLat && currentLong == pathLong{
                index = Int(i)
                break   //Breaking the loop when the index found
            }
        }
    
       //Creating new path from the current location to the destination
        let newPath = GMSMutablePath()    
        for i in index..<Int(self.path.count()){
            newPath.add(self.path.coordinate(at: UInt(i)))
        }
        self.path = newPath
        self.polyline.map = nil
        self.polyline = GMSPolyline(path: self.path)
        self.polyline.strokeColor = UIColor.darkGray
        self.polyline.strokeWidth = 2.0
        self.polyline.map = self.mapView
    }
    

    纬度和经度是四舍五入的,因此如果用户在旅行地点附近。根据要求使用以下扩展来四舍五入到小数点后 3 位或更多位。

    extension Double {
    // Rounds the double to decimal places value
    func rounded(toPlaces places:Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded() / divisor
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      • 2018-11-02
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多