在两个位置之间划一条线google map swift 3

时间:2017-05-11 17:27:55

标签: ios swift google-maps swift3 gmsmapview

我的api返回两个经度和纬度的位置。我试图这样做,但我的地图停留在欧洲没有任何行动或标记。

    @IBOutlet weak var mapView: GMSMapView!


     override func viewDidLoad() {
       super.viewDidLoad()

    let origin = "36.447139, -100.444653"
    let destination = "36.448799, -100.468546"


    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"

    Alamofire.request(url).responseJSON { response in

        print(response.request as Any)  // original URL request
        print(response.response as Any) // HTTP URL response
        print(response.data as Any)     // server data
        print(response.result as Any)   // result of response serialization

        let json = JSON(data: response.data!)
        let routes = json["routes"].arrayValue

        // print route using Polyline
        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.strokeWidth = 4
            polyline.strokeColor = UIColor.red
            polyline.map = self.mapView
        }

    }

0 个答案:

没有答案