将 Google 路线转换为 MKRoute

问题描述

我正在尝试从 Google Directions API 获取路线,以便我可以在我的应用中执行与 AR 相关的操作。我没有从 Apple 地图获取路线,因为只有少数国家/地区支持允许从 Apple 地图查看路线(可用国家/地区:http://www.apple.com/ios/feature-availability/#maps-directions

我目前正在使用此代码获取从起点到终点的路线:

func getLocation() {
        let origin = "\(37.403137),\(-121.977797)"
        let destination = "\(37.406401),\(-121.966639)"
        let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=MY-API-KEY"

        AF.request(url).responseJSON { (response) in
            
            do {
                let json = try JSON(data: response.data!)
                let routes = json["routes"].arrayValue
                
                for route in routes
                {
                    let routeOverviewpolyline = route["overview_polyline"].dictionary
                    let points = routeOverviewpolyline?["points"]?.stringValue
                    let path = GMSPath.init(fromEncodedpath: points!)

                    let polyline = GMSpolyline(path: path)
                    polyline.strokeColor = .black
                    polyline.strokeWidth = 10.0

                }
            } catch {
                print("error")
            }
        }
    }

我需要将该路由转换为 MKRoute,以便我可以在我的项目中使用我当前正在处理的库。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)