-3

I am having trouble rendering the JSON response onto the map. I am trying to make an app that you can enter in the a destination and google maps will make a route based on your current location.

I successfully can print out the JSON response in the console

But I am unsure as to how I can make a route using a polyline.

Everything I look at online is out of date

any help would be great thanks.

If you could point me to a tutorial it would be great!

thanks :)

mick1996
  • 516
  • 9
  • 31

1 Answers1

1

This is how I would answer it:

  func drawPath()
    {
        let origin = "\(43.1561681),\(-75.8449946)"
        let destination = "\(38.8950712),\(-77.0362758)"


        let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=API_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

            do {
                let json = try 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
                }
            }
            catch {
                print("ERROR: not working")
            }



        }

    }