gpt4 book ai didi

ios - 如何在 iOS Swift 中的 GoogleMaps 中绘制 CurrentLocation(locationStart) 到 SearchedLocation(locationEnd) 之间的路线

转载 作者:行者123 更新时间:2023-11-28 23:51:33 25 4
gpt4 key购买 nike

在我的代码中,我正在获取当前位置和搜索位置,但我无法在当前位置和搜索位置之间绘制路线。当我正在录制 findRoutrButton 时,即使我使用了

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

最佳答案

在 GoogleMap 上的两个标记之间绘制多段线

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

关于ios - 如何在 iOS Swift 中的 GoogleMaps 中绘制 CurrentLocation(locationStart) 到 SearchedLocation(locationEnd) 之间的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51962046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com