0

I'm trying to get a route between the origin coordinate and the destination coordinate.

I referenced stackoverflow.

So My code is below:

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

But as a result, response.data is below:

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

About this issue, I searched it and found that it may be caused when the coordinate can not be converted into the address.

But I did check both coordinates are converted into the right address by 'CLGeocoder'(reverseGeocodeLocation).

Finally, I did check at 'maps.google.com' that the same coordinates are addressed and the route is shown to me.

How can I solve this problem?

Community
  • 1
  • 1
LKM
  • 2,410
  • 9
  • 28
  • 53

2 Answers2

0

change mode walking to driving, i mean google cant find way for walking, but i offer use martix api like this https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=(currentLat),(currentLng)&destinations=(restLat),(restLng)&key=MY_KEY&mode=driving

Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
  • Thankyou , but as you can see the answer above , only 'transit' option returns well – LKM Mar 13 '17 at 13:18
0

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.

Your response returned as

"available_travel_modes":["TRANSIT"]

So try changing mode=walking to &mode=transit

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70