我正在尝试在模拟器上显示我的当前位置,但是它显示了另一个位置
import UIKit
import MapKit
class DriverVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var myMap: MKMapView!
let locationManager = CLLocationManager()
var driverLocation : CLLocationCoordinate2D?
var userLocation : CLLocationCoordinate2D?
override func viewDidLoad() {
super.viewDidLoad()
intializeLocationManager()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locationManager.location?.coordinate {
print("location\(location)")
myMap.removeAnnotations(myMap.annotations)
driverLocation = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
let region = MKCoordinateRegion(center: driverLocation!, span: MKCoordinateSpanMake(0.07, 0.07))
myMap.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = driverLocation!
annotation.title = "driver location"
myMap.addAnnotation(annotation)
}
}
private func intializeLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
@IBAction func signOut(_ sender: UIBarButtonItem) {
if (AuthProvider.Instance.logOut()){
dismiss(animated: true, completion: nil)
}
else{
alertTheUser(title: "couldn't sign out", message: "try again")
}
}
@IBAction func answer(_ sender: UIButton) {
}
private func alertTheUser(title: String, message: String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(ok)
present(alert,animated: true, completion: nil)
}
}
我在使用说明中向Info.plist添加了位置,而模拟器的调试位置不是