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.
Example:
func getDiectionFromGoogle(startCoordinate: CLLocationCoordinate2D, toLocation: CLLocationCoordinate2D) {
var urlString = "https://maps.googleapis.com/maps/api/directions/json?origin=\(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
}
})
placesTask.resume()
}
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. https://developers.google.com/maps/documentation/directions/intro
// update on Oct 15
I guess you are encountering the problem with encoded path. Here is how I decode the path. I am pretty sure I am not doing the simplest way, but I just ignore it because it just works. haha
// you can see a key called polyline in somewhere in the dictionary
// polylineCoordinates are all the coordinates for your routes
// path can be used for polyline
if let polyline = (paths[i] as AnyObject).value(forKey: "polyline") as? NSDictionary {
if let points = polyline.value(forKey: "points") as? String {
let gmsPath = GMSPath(fromEncodedPath: points)
for index in 0 ..< gmsPath!.count() {
let coordinate = gmsPath!.coordinate(at: index)
self.polylineCoordinates.append(coordinate)
self.path.add(coordinate)
}
}
}
Here is a Google document of how to use polylines. I will not give you any code for this one. I am pretty sure you can solve it. Learning by doing right? But if you stuck with something, you can come back to me. I will try to help:) https://developers.google.com/maps/documentation/ios-sdk/shapes
I am not sure Google provide a navigation like the Google maps or not. I build my app's navigation by myself. I can try to help you with that if I can.
// update on Oct 20
You are almost there. The path[i] comes from here LOL. Really ugly code.
if let jsonResult = (try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary {
if let routes = jsonResult["routes"] as? NSArray {
if let legs = routes.value(forKey: "legs") as? NSArray {
if let steps = legs.value(forKey: "steps") as? NSArray {
if steps.count != 0 {
if let firstStep = steps.object(at: 0) as? NSArray {
if firstStep.count != 0 {
if let paths = firstStep.object(at: 0) as? NSArray {
if paths.count > 0 {
for i in 0 ..< paths.count {
To see my graph, path[0] is start coordinate to intersection1, path1 is intersection1 to intersection2 and path[2] is intersection2 to end coordinate.
Actually my self.path is useless, you can just decode the path from jsonResult. The reason why I use self.polylineCoordinates is because I want to get all the coordinates (like the red triangle in my graph) to create my own navigation. In jsonResult, you can see some keys called startAt (if I didn't remember it wrong) and endAt, they are coordinates which represents as the black triangle in my graph. The reason why you might need to get all the coordinates is because a path from intersection to another intersection may not be a straight line, it can be a curve like intersection1 to intersection2.