How to decode the Google Directions API polylines field into lat long points in Objective-C for iPhone?

后端 未结 13 977
孤街浪徒
孤街浪徒 2020-11-30 19:05

I want to draw routes on a map corresponding to directions JSON which I am getting through the Google Directions API: https://developers.google.com/maps/documentation/direct

相关标签:
13条回答
  • 2020-11-30 19:56

    If anybody else is trying to do this in swift, here's @RootCode's answer adapted to swift (2.3):

    let path = GMSMutablePath()
    let steps = directionsToShowOnMap.steps
    for (idx, step) in steps.enumerate() {
        path.addCoordinate(coordinateFromJson(step["start_location"]))
        if let polylinePoints = step["polyline"].string, subpath = GMSPath(fromEncodedPath: polylinePoints) {
            for c in 0 ..< subpath.count() {
                path.addCoordinate(subpath.coordinateAtIndex(c))
            }   
        }
        if idx == steps.count - 1 {
            path.addCoordinate(coordinateFromJson(step["end_location"]))
        }
    }
    let polyline = GMSPolyline(path: path)
    polyline.strokeColor = UIColor.blueColor()
    polyline.strokeWidth = 3
    polyline.map = mapView
    

    and then:

    private func coordinateFromJson(location: JSON) -> CLLocationCoordinate2D {
        return CLLocationCoordinate2DMake(location["lat"].double!, location["lng"].double!)
    }
    
    0 讨论(0)
提交回复
热议问题