简体   繁体   English

在GMSMapView Swift iOS上从GMSPolyline删除行进路径

[英]Remove travelled path from GMSPolyline on GMSMapView Swift iOS

I am using google distance 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;] to get route from start location to end location. 我正在使用google distance 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;]获取从起始位置到终止位置的路线。

I am using the following code to draw route between my start and destination location 我正在使用以下代码在起点和目的地位置之间绘制路线

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()
            }
        }
    }
}

As you can see I am initialising path with encoded path from the api. 如您所见,我正在使用api的编码路径初始化路径。 Now I want to remove travelled GMSPolyline from the overall path How can I do that? 现在,我想从整体路径中删除已旅行的GMSPolyline,该怎么办? My current intiuation is that it will be from didUpdateLocations Here's my code of didUpdateLocations method 我当前的提议是它将来自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()

}

And in updatePolyLineIfRequired I want to remove travelled poly lines updatePolyLineIfRequired中,我想删除行进的折线

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

    }
}

I want to implement solution like Uber or Careem where travelled drawn GMSPolyline gets removed till user current location. 我想实施Uber或Careem之类的解决方案,其中将行驶的绘制的GMSPolyline删除,直到用户当前位置为止。

Thanks in Advance PS I am using Alamofire SwiftyJSON 在此先感谢PS我正在使用Alamofire SwiftyJSON

There are two solutions for this:- 有两种解决方案:

  1. Calling Directions Api each time didUpdateLocations function is called.(Not efficient) 每次调用didUpdateLocations函数时,调用路线Api。(效率不高)
  2. Removing the travelled coordinates from the GMSPath. 从GMSPath中删除行进的坐标。

Calling Directions api will be not useful unless your request limit for Direction api is less. 除非您对Directions api的请求限制较少,否则调用Directions api不会有用。

For removing the travelled coordinates from the path:- 要从路径中删除行进的坐标:-

    //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
}

The lat and longs are rounded of so that if the user is nearby the travelled location. 纬度和经度四舍五入,以使用户在行进地点附近。 Use the following extension to round of upto 3 decimal places or more according to requirement. 根据要求,使用以下扩展名将其舍入到小数点后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
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM