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

时间:2015-04-17 15:08:53

标签: ios mkmapview mapkit

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

2 个答案:

答案 0 :(得分:2)

在Interface Builder中,单击MKMapView并单击“用户位置”复选框。

编程方式:

self.mapView.showsUserLocation = YES;

答案 1 :(得分:2)

正如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
}