這是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