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