1

I am using GoogleMaps directions for my iOS app to show a route between my current location and the selected place. For this I use a polyline, the problem is that it draws a straight line instead of a route.

Can anyone help with this?

Anjali Kevadiya
  • 3,567
  • 4
  • 22
  • 43
Kelvin
  • 107
  • 1
  • 8

1 Answers1

1

I solved my problem. Here is my code and explanation , if someone encounters the same problem.

After you have an url to get the directions from Google API directions. You do the following (I used the SwiftyJSON from github):

            let url = NSURL(string: urlString)
            let request = NSURLRequest(URL: url!)
            let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
            let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in

                if error == nil
                {
                    let swiftyJSON = JSON(data: data!)
                    let route = swiftyJSON["routes"].array
                    let routeDirection = route![0].dictionary

                    let placeInfo = routeDirection!["overview_polyline"]!.dictionaryValue

                    let points = placeInfo["points"]?.string
                    let path = GMSPath(fromEncodedPath: points!)
                    let pathLine = GMSPolyline(path: path)
                    pathLine.strokeColor = UIColor.blueColor()
                    pathLine.strokeWidth = 6
                    pathLine.map = self.GoogleMaps
                }
            }
            task.resume()
Kelvin
  • 107
  • 1
  • 8