如何在谷歌地图iOS Swift中的两个位置之间绘制路线

我在我的iOS Swift项目中使用谷歌地图。 我想绘制地图上两个位置之间的path(不是直线)。 任何想法如何做到这一点?

使用相机缩放swift 3.0中的谷歌地图显示两个位置之间的多个路线

let origin = "\(startLocation.coordinate.latitude),\(startLocation.coordinate.longitude)" let destination = "\(destinationLocation.coordinate.latitude),\(destinationLocation.coordinate.longitude)" let urlString = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=API_KEY" let url = URL(string: urlString) URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in if(error != nil){ print("error") }else{ do{ let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject] let routes = json["routes"] as! NSArray self.mapView.clear() OperationQueue.main.addOperation({ for route in routes { let routeOverviewPolyline:NSDictionary = (route as! NSDictionary).value(forKey: "overview_polyline") as! NSDictionary let points = routeOverviewPolyline.object(forKey: "points") let path = GMSPath.init(fromEncodedPath: points! as! String) let polyline = GMSPolyline.init(path: path) polyline.strokeWidth = 3 let bounds = GMSCoordinateBounds(path: path!) self.mapView!.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 30.0)) polyline.map = self.mapView } }) }catch let error as NSError{ print("error:\(error)") } } }).resume() 

在Swift 3中的GoogleMap上的两个标记之间绘制折线。

  // 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() } 

在地图上绘制折线。

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

这段代码将适合你。 不要忘记改变你的API键和模式(走路,驾驶)。

 func draw(src: CLLocationCoordinate2D, dst: CLLocationCoordinate2D){ let config = URLSessionConfiguration.default let session = URLSession(configuration: config) let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(src.latitude),\(src.longitude)&destination=\(dst.latitude),\(dst.longitude)&sensor=false&mode=walking&key=**YOUR_KEY**")! 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 preRoutes = json["routes"] as! NSArray let routes = preRoutes[0] as! NSDictionary let routeOverviewPolyline:NSDictionary = routes.value(forKey: "overview_polyline") as! NSDictionary let polyString = routeOverviewPolyline.object(forKey: "points") as! String DispatchQueue.main.async(execute: { let path = GMSPath(fromEncodedPath: polyString) let polyline = GMSPolyline(path: path) polyline.strokeWidth = 5.0 polyline.strokeColor = UIColor.green polyline.map = mapView }) } } catch { print("parsing error") } } }) task.resume() }