这是ddnv和达斯汀的答案,为我工作的组合:
的MapView是的MKMapView * MapView的名称;
在viewDidLoad中添加此行,请注意加载中可能有更多的行。这是 只是简化。
- (void) viewDidLoad
{
[self.mapView.userLocation addObserver:self
forKeyPath:@"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:nil];
}
然后创建地图移动到当前位置的实际上市方法:
// Listen to change in the userLocation
-(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; // Change these values to change the zoom
span.longitudeDelta = 1;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
不要忘记正确的dealloc和注销观察者:
- (void)dealloc
{
[self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
[self.mapView removeFromSuperview]; // release crashes app
self.mapView = nil;
[super dealloc];
}
我添加了一个CLLocationManager手动做到这一点,但在相同的时间甚至不火MapView绘制用户位置,所以它看起来不起眼。 我不明白为什么这将是如此困难做 – 2010-03-20 10:14:21