iOS)从谷歌路线api

时间:2017-03-13 11:52:39

标签: ios swift google-maps google-polyline google-direction

我试图获取原点坐标和目标坐标之间的路线。

我引用了stackoverflow

所以我的代码如下:

let url = "https://maps.googleapis.com/maps/api/directions/json?origin=37.496375,126.9546903&destination=37.48121,126.9505233&mode=walking&key=MY_KEY"
Alamofire.request(url).responseJSON 
{ response in
print(response.request)  // original URL request
print(response.response) // HTTP URL response
print(response.data)     // server data
print(response.result)   // result of response serialization

let json = 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.init(path: path)
    polyline.map = self.mapView
 }
}

但结果是,response.data如下:

{
 "status" : "ZERO_RESULTS",
 "available_travel_modes" :
 [
 "TRANSIT"
  ],
 "geocoded_waypoints" : [
{},
{}],"routes" : []}

关于这个问题,我搜索过它,发现当坐标无法转换为地址时可能会导致它。

但我确实检查了两个坐标是否被CLGeocoder'(reverseGeocodeLocation)转换为正确的地址。

最后,我确实查看了map.google.com'找到相同的坐标并向我显示路线。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

改变模式步行到驾驶,我的意思是谷歌找不到走路的方式,但我提供使用像这样的martix api https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=(currentLat),(currentLng)&destinations=(restLat),(restLng)&key=MY_KEY&mode=driving

答案 1 :(得分:0)

  

available_travel_modes包含一系列可用的旅行模式。   当请求指定旅行模式并获取时,将返回此字段   没有结果。该阵列包含可用的旅行模式   给定航点组的国家。

您的回复是

"available_travel_modes":["TRANSIT"]

因此,请尝试将mode=walking更改为&mode=transit