这是ddnv和达斯汀的答案的结合,对我有用:
mapView是MKMapView * mapView的名称;
在viewDidLoad中添加此行,请注意加载中可能会有更多行。这只是简化了。
- (void) viewDidLoad
{
[self.mapView.userLocation addObserver:self
forKeyPath:@"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:nil];
}
然后创建将地图移动到当前位置的实际列表方法:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta = 1;
span.longitudeDelta = 1;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
不要忘记适当地分配并注销观察者:
- (void)dealloc
{
[self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
[self.mapView removeFromSuperview];
self.mapView = nil;
[super dealloc];
}