【问题标题】:Add Custom Path To include in Google Directions API添加自定义路径以包含在 Google Directions API 中
【发布时间】:2018-04-13 03:28:18
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    使用此功能,您将获得两个位置之间的折线。

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

    【讨论】:

    • 我的问题是没有使用路线 API。我已经实现了这一点,并且能够获得从 x 到 y 的方向。我想要做的是添加(本地到我的设备或使用我的代码的其他设备)一种添加自定义路线的方法,假设 x 到 y 通常需要 6 分钟绕行 a 。建筑,而是穿过公园,需要 2 分钟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2015-02-03
    相关资源
    最近更新 更多