我正在创建一个需要指导的应用程序。将折线绘制到GoogleMap上后,我遇到了一个问题。我要显示的是:到位置的总距离,估计的行驶时间,到下一弯的距离,弯的街道以及一幅图像,该图像显示用户转向的方向。
我不知道该怎么做。所有帮助都会很棒!
这段代码是从我的Google API获取路线,然后在地图上显示折线的代码。
func getPolylineRoute(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D){
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(source.latitude),\(source.longitude)&destination=\(destination.latitude),\(destination.longitude)&key=APIKEY&sensor=false")!
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]{
guard let routes = json["routes"] as? NSArray else {
DispatchQueue.main.async {
}
return
}
if (routes.count > 0) {
let overview_polyline = routes[0] as? NSDictionary
let dictPolyline = overview_polyline?["overview_polyline"] as? NSDictionary
let points = dictPolyline?.object(forKey: "points") as? String
self.showPath(polyStr: points!)
DispatchQueue.main.async {
let bounds = GMSCoordinateBounds(coordinate: source, coordinate: destination)
let update = GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 170, left: 30, bottom: 30, right: 30))
self.mapView!.moveCamera(update)
}
}
else {
DispatchQueue.main.async {
}
}
}
}
catch {
print("error in JSONSerialization")
DispatchQueue.main.async {
}
}
}
})
task.resume()
}
这些出口是我希望向用户显示的。
@IBOutlet weak var directionImage: UIImageView!
@IBOutlet weak var totalDistanceD: UILabel!
@IBOutlet weak var totalTimeD: UILabel!
@IBOutlet weak var nextStreetD: UILabel!
@IBOutlet weak var distanceNxtStreetD: UILabel!