【发布时间】:2015-09-04 02:55:04
【问题描述】:
我一直在尝试让用户的当前位置显示在我的地图上,但一直未能成功。我是 MapKit 的新手,所以我可能遗漏了一些东西。
我确实收到了要求允许使用我当前位置的弹出警报,但实际上没有显示任何位置。
代码如下:
标题.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;
标签: objective-c mapkit currentlocation