我是IOS的新手。我正在尝试使用Maps来获取用户的当前位置。我关注的教程适用于IOS 10。
我经历了这篇文章并完成了它所说的一切,但它仍然无法正常工作Location Services not working in iOS 11
以下是我的代码
class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {
var locationManager=CLLocationManager()
// @IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate=self
locationManager.desiredAccuracy=kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0]
let latitude=userLocation.coordinate.latitude
let longitude=userLocation.coordinate.longitude
let latDelta:CLLocationDegrees=0.05
let lonDelta:CLLocationDegrees=0.05
let span=MKCoordinateSpan(latitudeDelta:latDelta, longitudeDelta:lonDelta)
let location=CLLocationCoordinate2D(latitude:latitude,longitude:longitude)
let region=MKCoordinateRegion(center:location,span:span)
self.mapView.setRegion(region,animated:true)
}
}
我在info.plist文件中添加了以下内容
<key>NSLocationAlwaysUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
任何帮助将不胜感激。