您好,我想知道是否有一种方法可以在谷歌地图 iOS 中的两个或多个标记之间绘制航路点。我不想画直线...而是只使用公共道路。这是我的一些绘制直线的代码,但这不是我想要的。
@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
}
Run Code Online (Sandbox Code Playgroud)
在 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
guard error == nil else {
print(error!.localizedDescription) // Handle error here
return
}
do {
guard let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] else {
return
}
guard let routes = json["routes"] as? [Any] else { return }
guard !routes.isEmpty else { return }
guard let overview_polyline = routes[0] as? [String: Any] else { return }
guard let polyString = overview_polyline["points"] as? String else { return }
//Call this below method to draw path on map
self.showPath(polyStr: polyString!)
}catch{
print("error in JSONSerialization \(error)")
}
})
task.resume()
}
Run Code Online (Sandbox Code Playgroud)
在地图上绘制折线。
func showPath(polyStr :String){
let path = GMSPath(fromEncodedPath: polyStr)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 3.0
polyline.map = mapView // Your map view
}
Run Code Online (Sandbox Code Playgroud)