添加自定义路径包含在Google Directions API中

时间:2018-04-13 03:28:18

标签: swift google-places-api google-maps-sdk-ios google-directory-api

所以我创建了一个应用程序(在iOS上),使用谷歌地图SDK,谷歌放置API,谷歌方向API,以及其他一些东西来运行它。我可以从当前位置获取路线 - >任何地方并展示它们。我想知道是否有任何方法可以添加自定义路径,这些路径将合并到谷歌路线api中,所以当我通过谷歌路线API检索路线方向时,这些路径包含在路线方向中。例如,穿过城市中的1条街道和城市中的另一条街道之间的公园的路径。我不是在寻找任何代码,只是一个指南或文档来开始。

1 个答案:

答案 0 :(得分:0)

  

使用此功能,您将在两个地方之间获得Polyline。

func getPolylineRoute(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D){

        let config = URLSessionConfiguration.default
        let session = URLSession(configuration: config)
        let url = URL(string: "http://maps.googleapis.com/maps/api/directions/json?origin=\(source.latitude),\(source.longitude)&destination=\(destination.latitude),\(destination.longitude)&sensor=false&mode=driving")!
        print(url)
        let task = session.dataTask(with: url, completionHandler: {
            (data, response, error) in
            if error != nil {
                print(error!.localizedDescription)
            }else{
                do {
                    if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{

                        DispatchQueue.main.async {
                            let routes = json["routes"] as? NSArray
                            self.ViewMap.clear()
                            let routeOverviewPolyline:NSDictionary = (routes![0] as! NSDictionary).value(forKey: "overview_polyline") as! NSDictionary
                            let points = routeOverviewPolyline.object(forKey: "points")
                            let path = GMSPath.init(fromEncodedPath: points! as! String)
                            let polyline = GMSPolyline.init(path: path)
                            polyline.strokeWidth = 3
                            let bounds = GMSCoordinateBounds(path: path!)
                            self.ViewMap!.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 30.0))
                            polyline.map = self.ViewMap
                        }
                    }
                }catch{
                    print("error in JSONSerialization")
                }
            }
        })
        task.resume()
    }
  

使用这样的功能: -

getPolylineRoute(from: CLLocationCoordinate2DMake(23.0350073, 72.5293244), to: CLLocationCoordinate2DMake(23.0497364, 72.5117241))