Draw polyline using Google Maps in custom view with Swift 3

后端 未结 3 1830
渐次进展
渐次进展 2020-12-10 07:24

I am trying to draw route between two places using Google Maps on a custom UIView but not able to get it correctly implemented. My custom view is mapViewX. I\'ve installed g

3条回答
  •  孤城傲影
    2020-12-10 08:02

    It works fine here. Make sure you're setting correct coordinates of GMSCameraPosition.

    EDIT

    To draw the route between two coordinate, use Google Maps Direction API

    Something like :

        let origin = "\(37.778483),\(-122.513960)"
        let destination = "\(37.706753),\(-122.418677)"
        let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=[YOUR-API-KEY]"
    
        Alamofire.request(url).responseJSON { response in
            let json = 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(path: path)
                polyline.strokeColor = .black
                polyline.strokeWidth = 10.0
                polyline.map = mapViewX
    
            }
        }
    

    For more info - Directions API Developer's Guide

提交回复
热议问题