如何在没有CLLocationManager的情况下将MKMapView放大到用户当前位置?
有了MKMapView有一个选项叫“显示用户当前位置”,它会自动显示用户在map上的位置。 
我想移动并放大到这个位置,当它被发现(如果它改变)。
 问题是,当用户位置在map上更新时,没有任何方法可以调用,所以我无处放置zoom/scroll的代码。 
 当MKMapView (或更新)用户位置时是否有通知的方法,以便我可以移动/缩放它? 如果我使用我自己的CLLocationManager ,我得到的更新与地图上用户标记的更新不符,所以当我的地图移动并在蓝色针脚出现之前放大几秒钟时,看起来很愚蠢。 
这感觉就像基本的function,但我花了几个星期寻找一个解决scheme,没有任何closures。
 您必须注册MKMapView的userLocation.location属性的KVO通知。 
 为了做到这一点,把这个代码放在你的ViewController的viewDidLoad:或者你的地图视图初始化的地方。 
 [self.mapView.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:NULL]; 
然后执行此方法来接收KVO通知
 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([self.mapView showsUserLocation]) { [self moveOrZoomOrAnythingElse]; // and of course you can use here old and new location values } } 
 这段代码适合我。 
 顺便说一下, self是我的ViewController在这种情况下。 
这是ddnv和达斯汀的答案,这对我有效的组合:
mapView是MKMapView * mapView的名称;
在viewDidLoad中添加这一行,注意在负载中可能有更多的行。 这只是简化。
 - (void) viewDidLoad { [self.mapView.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil]; } 
然后创build将地图移动到当前位置的实际列表方法:
 // 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]; } 
不要忘记正确释放并取消注册观察者:
 - (void)dealloc { [self.mapView.userLocation removeObserver:self forKeyPath:@"location"]; [self.mapView removeFromSuperview]; // release crashes app self.mapView = nil; [super dealloc]; } 
由于iOS 5.0 Apple已经为MKMapView添加了一个新的方法。 这种方法正是你想要的和更多。
看看: https : //developer.apple.com/documentation/mapkit/mkmapview
 - (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated; 
 您可以通过实现MKMapViewDelegate协议来监视MKMapView何时更新地图上的用户位置。 只要执行: 
 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { CLLocationAccuracy accuracy = userLocation.location.horizontalAccuracy; if (accuracy ......) { } } 
这个callback应该完全与地图上显示的内容同步。
尝试这个:
 [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; 
没问题…在你的UIViewController的子类中有MKMapView的viewDidLoad方法添加这个(假设你的MKMapView被命名为map):
 CLLocation *location = [[[CLLocation alloc] initWithLatitude:map.centerCoordinate.latitude longitude:map.centerCoordinate.longitude] autorelease]; //Get your location and create a CLLocation MKCoordinateRegion region; //create a region. No this is not a pointer region.center = location.coordinate; // set the region center to your current location MKCoordinateSpan span; // create a range of your view span.latitudeDelta = BASE_RADIUS / 3; // span dimensions. I have BASE_RADIUS defined as 0.0144927536 which is equivalent to 1 mile span.longitudeDelta = BASE_RADIUS / 3; // span dimensions region.span = span; // Set the region's span to the new span. [map setRegion:region animated:YES]; // to set the map to the newly created region