如何在 MKMapView 上显示默认位置指示器



有没有办法像在iPhone上的标准地图应用程序中一样在MKMapView上显示当前位置的默认蓝色指示器?

在界面生成器中,单击 MKMapView 并单击"用户位置"复选框。

以编程方式:

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
}

相关内容

最新更新