0

I would like to know all possible routes between two coordinate , google map api gives me only one route, using this url: "https://maps.googleapis.com/maps/api/directions/json?origin=30.1124,31.4003&destination=29.9792,31.1342&provideRouteAlternatives=true&key=xx")

i can't find where are all the routes and to get them

here is the code

func getRoutes(handler:@escaping (_ error: String?) -> Void){
let request = URLRequest(url: URL(string:   "https://maps.googleapis.com/maps/api/directions/json?origin=30.1124,31.4003&destination=29.9792,31.1342&provideRouteAlternatives=true&key=AIzaSyAf5emsTReEhPgC3NwAnXEdoa_CllLbyLc")!)

 //   request.addValue("provideRouteAlternatives", forHTTPHeaderField: "true")

    let session = URLSession.shared
    let task = session.dataTask(with: request) { data, response, error in
        if error != nil {
            // Handle error...
            handler("Connection Error")
            return
        }
        let parsedResult: [String: AnyObject]!
        do {
            parsedResult = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: AnyObject]
            print(parsedResult)
        } catch {
            print("Error parsing result as JSON")
            handler("Cant download Student data")
            return
        }

        if let array = parsedResult["routes"] as? NSArray {
            if let routes = array[0] as? NSDictionary{
                if let overview_polyline = routes["overview_polyline"] as? NSDictionary{
                    if let points = overview_polyline["points"] as? String{
                        print(points)
                        // Use DispatchQueue.main for main thread for handling UI
                        DispatchQueue.main.async {
                            // show polyline
                            let path = GMSPath(fromEncodedPath:points)
                            let polyline = GMSPolyline(path: path)
                            //self.polyline.path = path
                            polyline.strokeWidth = 4
                            polyline.map = self.myMap
                        }
                    }
                }
            }
        }

        handler(nil)

    }
      task.resume()
}
  • Possible duplicate of [Drawing Route Between Two Places on GMSMapView in iOS](https://stackoverflow.com/questions/22550849/drawing-route-between-two-places-on-gmsmapview-in-ios) – Hardik Thakkar May 02 '18 at 07:03

1 Answers1

1

All the routes are present in the routes array in the JSON response.

In the case of particular origin and destination you specified, I see only one element in the routes array in the API response. But https://www.google.co.in/maps/dir/'30.1124,31.4003'/'29.9792,31.1342'/ shows multiple routes on the web version.

There is nothing you can probably do in your program to get all the routes unless Google provides it in the JSON response.

Davis
  • 85
  • 2
  • 8
  • thanx a lot for ur help – basma mahmoud May 01 '18 at 17:01
  • have u try to find this answer first before post question? you can try on this link contains answer for the same. https://stackoverflow.com/questions/22550849/drawing-route-between-two-places-on-gmsmapview-in-ios/45033477#45033477 – Hardik Thakkar May 02 '18 at 07:02
  • thanx fro your reply, yes i did search before posting my question, and i didn't find an answer. but i figured it out i have to add alternatives = true to my url. the one you post doesn't have my answer :) – basma mahmoud May 02 '18 at 07:32
  • @basma-mahmoud Were you able to solve your problem? I have been trying to add parameters such as `provideRouteAlternatives=true` and `alternatives=true` and none of them worked. – MrEduar Aug 23 '21 at 17:03