关于堆栈溢出的所有与此类似的问题似乎都是针对Java和Android的。
我使用MKMapView控件具有以下代码。
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = true
mapView.setUserTrackingMode(.followWithHeading, animated: true)
let locationCoordinate = CLLocationCoordinate2D(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
currentLocationCoordinateRegion = MKCoordinateRegion(center: locationCoordinate, latitudinalMeters: 200, longitudinalMeters: 200)
mapView.setRegion(currentLocationCoordinateRegion, animated: true)
print("latitude:", mapView.userLocation.coordinate.latitude, "longitude:", mapView.userLocation.coordinate.longitude)
}
我使用的是实际设备,因此地图视图应显示我的当前位置,而打印语句应打印我的当前位置,但是我在地图视图上看到的是蓝色,并且打印结果显示的纬度为0.0,且经度在调试窗口中为0.0:
纬度:0.0经度:0.0
为什么要这样做,我应该怎么做才能解决这个问题?
我在工具栏上还有一个条形按钮项,其代码如下:
@IBAction func actionShow(_ sender: UIBarButtonItem) {
print("latitude:", mapView.userLocation.coordinate.latitude, "longitude:", mapView.userLocation.coordinate.longitude)
let locationCoordinate = CLLocationCoordinate2D(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
currentLocationCoordinateRegion = MKCoordinateRegion(center: locationCoordinate, latitudinalMeters: 200, longitudinalMeters: 200)
mapView.setRegion(currentLocationCoordinateRegion, animated: true)
}
产生相同的结果。
答案 0 :(得分:1)
您必须请求访问用户位置的权限,这是您在viewDidLoad
中执行的第一项操作,一旦获得权限,就继续显示用户位置,遵循UIViewController可以帮助您
class ViewController: UIViewController {
@IBOutlet var mapView: MKMapView!
var locationManager: CLLocationManager?
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.setUserTrackingMode(.followWithHeading, animated: true)
self.checkLocationAuthorization()
}
func checkLocationAuthorization(authorizationStatus: CLAuthorizationStatus? = nil) {
switch (authorizationStatus ?? CLLocationManager.authorizationStatus()) {
case .authorizedAlways, .authorizedWhenInUse:
mapView.showsUserLocation = true
case .notDetermined:
if locationManager == nil {
locationManager = CLLocationManager()
locationManager!.delegate = self
}
locationManager!.requestWhenInUseAuthorization()
default:
print("Location Servies: Denied / Restricted")
}
}
}
extension ViewController: MKMapViewDelegate, CLLocationManagerDelegate {
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
let region = MKCoordinateRegion(center: userLocation.coordinate, latitudinalMeters: 200, longitudinalMeters: 200)
mapView.setRegion(region, animated: true)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
self.checkLocationAuthorization(authorizationStatus: status)
}
}
您还必须在Info.plist
中包含以下属性,如果没有这些locationManager
,这些属性将不会继续请求授权。
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Message for AlwaysAndWhenInUseUsageDescription</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Message for AlwaysUsageDescription</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Message for WhenInUseUsageDescription</string>