我一直在試圖讓用戶的當前位置顯示在我的地圖上,但一直未能。我是MapKit的新手,所以我可能錯過了一些東西。iOS地圖顯示用戶的當前位置
我確實得到彈出警報詢問是否允許使用我的當前位置,但是實際上沒有顯示位置。
下面的代碼:
頁眉.H:
@interface TeachersMapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
@property (nonatomic,retain) CLLocationManager *locationManager;
@property(nonatomic, retain) IBOutlet MKMapView *mapView;
@end
實施
- (void)viewDidLoad
{
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER)
{
[self.locationManager requestWhenInUseAuthorization];
}
#endif
self.locationManager.delegate=self;
[self.locationManager startUpdatingLocation];
[self.mapView.userLocation addObserver:self
forKeyPath:@"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:NULL];
}
任何幫助,這將是非常讚賞。
在MapView上,您需要將showUserCurrentLocation屬性設置爲YES。 mapView.showsUserLocation = YES; –