iOS) 从 google 方向 api 的两个坐标得不到路线
iOS) Getting no route from two coordinates from google directions api
我正在尝试获取起点坐标和终点坐标之间的路线。
我引用了 Whosebug。
所以我的代码如下:
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) 转换为正确的地址。
最后,我确实在 'maps.google.com' 检查了相同的坐标,并向我显示了路线。
我该如何解决这个问题?
将步行模式更改为驾车,我的意思是 google 无法找到步行方式,但我提供像这样使用 martix api
https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=(currentLat),(currentLng)&destinations=(restLat),(restLng)&key=MY_KEY&mode=driving
available_travel_modes contains an array of available travel modes.
This field is returned when a request specifies a travel mode and gets
no results. The array contains the available travel modes in the
countries of the given set of waypoints.
您的回复返回为
"available_travel_modes":["TRANSIT"]
所以尝试将 mode=walking
更改为 &mode=transit
我正在尝试获取起点坐标和终点坐标之间的路线。
我引用了 Whosebug。
所以我的代码如下:
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) 转换为正确的地址。
最后,我确实在 'maps.google.com' 检查了相同的坐标,并向我显示了路线。
我该如何解决这个问题?
将步行模式更改为驾车,我的意思是 google 无法找到步行方式,但我提供像这样使用 martix api https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=(currentLat),(currentLng)&destinations=(restLat),(restLng)&key=MY_KEY&mode=driving
available_travel_modes contains an array of available travel modes. This field is returned when a request specifies a travel mode and gets no results. The array contains the available travel modes in the countries of the given set of waypoints.
您的回复返回为
"available_travel_modes":["TRANSIT"]
所以尝试将 mode=walking
更改为 &mode=transit