SWIFT:谷歌地图绘制航点折线 [英] SWIFT: google maps draw waypoint polyline

查看:108
本文介绍了SWIFT:谷歌地图绘制航点折线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道是否有方法在谷歌地图iOS中的两个或多个标记之间绘制一个航点。我不想画直线......但只使用公共道路。

  @objc private func makeGpsPath()这里是我的一些代码来绘制直线,但它不是我正在寻找的。 {
for i in 0 ..< trailArr.count {
path.add(trailArr [i])
}
让polyline = GMSPolyline(路径:路径)
polyline.strokeWidth = 5.0
折线。 strokeColor = UIColor.black
polyline.map = mapViewContainer
}


解决

  //将您的源代码和目标坐标在这个方法中。 
func getPolylineRoute(从源代码:CLLocationCoordinate2D到目的地:CLLocationCoordinate2D){

让config = URLSessionConfiguration.default
让session = URLSession(configuration:config)

let url = URL(string:http://maps.googleapis.com/maps/api/directions/json?origin=\(source.latitude),\\((source.longitude )&destination= \(destination.latitude),\(destination.longitude)及传感器=假安培;模式=驱动)!

让task = session.dataTask(with:url,completionHandler:{
(data,response,error)in
if error!= nil {
print(错误!.localizedDescription)
} else {
do {
如果让json:[String:Any] = try JSONSerialization.jsonObject(with:data !, options:.allowFragments)as?[字符串:任意] {

让routes = json [routes] as?[Any]
let overview_polyline = routes?[0] as?[String:Any]
让polyString = overview_polyline?[points] as?String

//调用此方法在地图上绘制路径
self.showPath(polyStr:polyString!)
}
$ b $ catch {
print(JSONSerialization中的错误)
}
}
})
task.resume()
}

在地图上绘制折线。

  func showPath(polyStr:String){
let path = GMSPath(fromEncodedPath:polyStr)
let polyline = GMSPolyline(path:path)
polyline。 strokeWidth = 3.0
polyline.map = mapView //您的地图视图
}


Hi I wonder if there is a method to draw a waypoint between two or more markers in google maps iOS. I don't want to draw straight lines... but use just public roads. Here is some of my code to draw straight lines but its not what i am looking for.

    @objc private func makeGpsPath(){
    for i in 0 ..< trailArr.count {
        path.add(trailArr[i])
    }
    let polyline = GMSPolyline(path: path)
    polyline.strokeWidth = 5.0
    polyline.strokeColor = UIColor.black
    polyline.map = mapViewContainer
}

解决方案

To draw polyline between two markers on GoogleMap in Swift 3.

// Pass your source and destination coordinates in this method.
func getPolylineRoute(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D){

        let config = URLSessionConfiguration.default
        let session = URLSession(configuration: config)

        let url = URL(string: "http://maps.googleapis.com/maps/api/directions/json?origin=\(source.latitude),\(source.longitude)&destination=\(destination.latitude),\(destination.longitude)&sensor=false&mode=driving")!

        let task = session.dataTask(with: url, completionHandler: {
            (data, response, error) in
            if error != nil {
                print(error!.localizedDescription)
            }else{
                do {
                    if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{

                        let routes = json["routes"] as? [Any]
                        let overview_polyline = routes?[0] as?[String:Any]
                        let polyString = overview_polyline?["points"] as?String

                        //Call this method to draw path on map
                        self.showPath(polyStr: polyString!)
                    }

                }catch{
                    print("error in JSONSerialization")
                }
            }
        })
        task.resume()
    }

To draw polyline on map .

func showPath(polyStr :String){
    let path = GMSPath(fromEncodedPath: polyStr)
    let polyline = GMSPolyline(path: path)
    polyline.strokeWidth = 3.0
    polyline.map = mapView // Your map view
}

这篇关于SWIFT:谷歌地图绘制航点折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆