skyleguy skyleguy - 13 days ago 5
iOS Question

Use GoogleMaps SDK to generate routes?

I am very new and struggling with the Google Maps sdk. Is it possible to use the google maps sdk to actually generate a route? Like if I could make a button that said "Generate Route" and then once clicked it generates a route that the user could actually navigate like they were using the real google maps app? Thanks.



func getDiectionFromGoogle(startCoordinate: CLLocationCoordinate2D, toLocation: CLLocationCoordinate2D) {
    var urlString = "\(startCoordinate.latitude),\(startCoordinate.longitude)&destination=\(toLocation.latitude),\(toLocation.longitude)&key=\("your key!!")"
    urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
    let session = URLSession.shared
    let placesTask = session.dataTask(with: URL(string: urlString)!, completionHandler: {data, response, error in
        if error != nil {
        if let jsonResult = (try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary {

            // *******UPDATE**********
            print(jsonResult) // this returns a dictionary

            if let routes = jsonResult["routes"] as? NSArray {
                // example to get data from dictionary


See what you get in jsonResult. You can find all the coordinates you need to generate paths for your direction and more!

Also, you can use more parameters in your json request. Take a look here.