如何在 iOS Swift 的 GoogleMaps 中绘制当前位置(位置开始)和搜索位置(位置结束)之间的路由



在我的代码中,我正在获取当前位置和搜索位置,但我无法在当前位置和搜索位置之间绘制路线。 当我在录制查找路由按钮时,即使我使用 我也遇到了错误

DispatchQueue.main.async{ }

错误:由于未捕获的异常"GMSThreadException"而终止应用,原因:"必须从主线程调用 API 方法">

请帮助我解决我的错误。

以下是我的总代码

import UIKit
import GoogleMaps
import GooglePlaces
import CoreLocation
enum Location {
case currentLocation
case destinationLocation
}
class ViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var googleMapView: GMSMapView!
@IBOutlet weak var destinationTextfield: UITextField!
var locationManager = CLLocationManager()// for current location
var locationSelected = Location.currentLocation
var locationStart = CLLocation()
var locationEnd = CLLocation()
override func viewDidLoad() {
super.viewDidLoad()
//Your map initiation code
self.googleMapView?.isMyLocationEnabled = true
//Location Manager code to fetch current location
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
//Location Manager delegates
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 15.0)
self.googleMapView?.animate(to: camera)
print("user latitude = (location?.coordinate.latitude)")
print("user longitude = (location?.coordinate.longitude)")
locationStart = CLLocation(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
//Finally stop updating location otherwise it will come again and again in this delegate
self.locationManager.stopUpdatingLocation()
self.googleMapView.settings.myLocationButton = true
}
func drawPath(currentLocation: CLLocation, searchedLocation: CLLocation)
{
let origin = "(currentLocation.coordinate.latitude),(currentLocation.coordinate.longitude)"
let destination = "(searchedLocation.coordinate.latitude),(searchedLocation.coordinate.longitude)"
let urlString = "https://maps.googleapis.com/maps/api/directions/json?origin=(origin)&destination=(destination)&mode=driving&key=APIKEY"
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.googleMapView.clear()
OperationQueue.main.addOperation({
DispatchQueue.main.async
{
for route in routes
{
DispatchQueue.main.async
{
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.googleMapView!.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 30.0))
polyline.map = self.googleMapView
}
}   
}
})
}catch let error as NSError{
print("error:(error)")
}
}
}).resume()
}
//for searching destination
@IBAction func destinationButton(_ sender: Any) {
let autoCompleteController = GMSAutocompleteViewController()
autoCompleteController.delegate = self
locationSelected = .destinationLocation
// Change text color
UISearchBar.appearance().setTextColor(color: UIColor.black)
self.locationManager.stopUpdatingLocation()
self.present(autoCompleteController, animated: true, completion: nil)
}
//finding route button
@IBAction func findRoutrButton(_ sender: Any) {
DispatchQueue.main.async
{
self.drawPath(currentLocation: self.locationStart, searchedLocation: self.locationEnd)
}
}
}

在谷歌地图上的两个标记之间绘制折线

func getPolylineRoute(from source: CLLocation, to destination: CLLocation){
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?["overview_polyline"] as?[String:Any]
let points = polyString?["points"] as? String
//draw polyline on map 
let path = GMSPath(fromEncodedPath: points!)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 3.0
polyline.map = mapView // Your map view
}
}catch{
print("error in JSONSerialization")
}
}
})
task.resume()
}

相关内容

  • 没有找到相关文章

最新更新