快速浏览一下文档就会发现其中的魔力。
设置userTrackingMode
您的地图到MKUserTrackingModeFollow
.
See here http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html.
Update:
由于您更新了问题,因此这是新答案。
为了将地图重新定位到用户位置,我建议编写一个简单的帮助方法:
- (void)recenterUserLocation:(BOOL)animated{
MKCoordinateSpan zoomedSpan = MKCoordinateSpanMake(1000, 1000);
MKCoordinateRegion userRegion = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, zoomedSpan);
[self.mapView setRegion:userRegion animated:animated];
}
现在,如果用户停止移动地图,您应该在短暂延迟后调用它。您可以在regionDidChange
mapView 的委托方法。
但是,如果用户在真正重置地图之前多次更改区域,并且不锁定重置方法,则可能会出现问题。因此,如果可以使地图居中,那么制作一个标志是明智的。就像一个财产BOOL canRecenter
.
初始化它YES
并更新recenterUserLocation
方法:
- (void)recenterUserLocation:(BOOL)animated{
MKCoordinateSpan zoomedSpan = MKCoordinateSpanMake(1000, 1000);
MKCoordinateRegion userRegion = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, zoomedSpan);
[self.mapView setRegion:userRegion animated:animated];
self.canRecenter = YES;
}
现在,在用户以任何方式移动地图后,您可以安全地调用它,并且延迟很小:
- (void)mapView:(MKMapView *)mMapView regionDidChangeAnimated:(BOOL)animated{
if (self.canRecenter){
self.canRecenter = NO;
[self performSelector:@selector(recenterUserLocation:) withObject:@(animated) afterDelay:3];
}
}