我有一个嵌入在 View Controller 中的 mapView,我想显示用户的位置。在以下编译过程中,加载后的 map 什么都不做,也不显示用户的位置。我先在 viewDidLoad 中尝试了代码,然后在 viewDidFishingLoadingMap 中尝试了代码,因为我读到你需要先让 map 加载,但它仍然什么都不做。
除了设置跟踪模式以使 map 以用户位置为中心之外,是否还有任何其他步骤?我找到了 various approaches在 Objective-C 中使用 mapView.showsUserLocation = YES;
或 [self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
} 但以下内容似乎被推荐用于 Swift。
func mapViewDidFinishLoadingMap(_ mapView: MKMapView){
mapView.setUserTrackingMode(.follow, animated:true)
}
最佳答案
- 使用 Interface Builder 或代码启用用户位置
- 将
Privacy - Location Always and When In Use Usage Description
(或您喜欢的任何其他位置使用模式) key 添加到 info.plist 文件 - 导入
CoreLocation
- 检查授权状态并在需要位置访问权限时提示用户。
swift
if CLLocationManager.authorizationStatus() == .notDetermined {
CLLocationManager().requestWhenInUseAuthorization()
}
objective-c
[CLLocationManager requestWhenInUseAuthorization];
关于ios - 在 MapKit 中显示用户位置所需的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52882966/