如何在 MKMapView 上显示默认位置指示器
How to show default location indicator on MKMapView
有没有办法像 iphone 上的标准地图应用程序那样在 MKMapView 上显示当前位置的默认蓝色指示器?
在 Interface Builder 中,单击 MKMapView 并单击 "User Location" 复选框。
程序化:
self.mapView.showsUserLocation = YES;
正如 user3870739 所写,您必须将 showUserLocation 设置为 true。
此外,您必须确保用户位置在地图的可见区域中:
let userLocation = mapView.userLocation
if userLocation.location != nil{
let region = MKCoordinateRegionMakeWithDistance(
userLocation.location.coordinate, 2000, 2000)
mapView.setRegion(region, animated: true)
}
而且,当然,您必须先征得许可才能获取位置:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//ask for location authorizaion
locationManager = CLLocationManager()
locationManager?.requestWhenInUseAuthorization()
return true
}
有没有办法像 iphone 上的标准地图应用程序那样在 MKMapView 上显示当前位置的默认蓝色指示器?
在 Interface Builder 中,单击 MKMapView 并单击 "User Location" 复选框。
程序化:
self.mapView.showsUserLocation = YES;
正如 user3870739 所写,您必须将 showUserLocation 设置为 true。
此外,您必须确保用户位置在地图的可见区域中:
let userLocation = mapView.userLocation
if userLocation.location != nil{
let region = MKCoordinateRegionMakeWithDistance(
userLocation.location.coordinate, 2000, 2000)
mapView.setRegion(region, animated: true)
}
而且,当然,您必须先征得许可才能获取位置:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//ask for location authorizaion
locationManager = CLLocationManager()
locationManager?.requestWhenInUseAuthorization()
return true
}