How do I zoom an MKMapView to the users current location without CLLocationManager?

IphoneMkmapview

Iphone Problem Overview


With the MKMapView there's an option called "Show users current location" which will automatically show a users location on the map.

I'd like to move and zoom to this location when it's found (and if it changes).

The problem is, there doesn't appear to be any method called when the user location is updated on the map, so I have nowhere to put the code that will zoom/scroll.

Is there a way to be notified when an MKMapView has got (or updated) the user location so I can move/zoom to it? If I use my own CLLocationManager the updates I get do not correspond with the updates of the user marker on the map, so it looks silly when my map moves and zooms seconds before the blue pin appears.

This feels like basic functionality, but I've spent weeks looking for a solution and not turned up anything close.

Iphone Solutions


Solution 1 - Iphone

You have to register for KVO notifications of userLocation.location property of MKMapView.

To do this, put this code in viewDidLoad: of your ViewController or anywhere in the place where your map view is initialized.

[self.mapView.userLocation addObserver:self  
        forKeyPath:@"location"  
           options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)  
           context:NULL];

Then implement this method to receive KVO notifications

- (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
    }
}

This code works fine for me.
BTW, self is my ViewController in this context.

Solution 2 - Iphone

This is a combination of ddnv and Dustin's answer which worked for me:

mapView is the name of the MKMapView *mapView;

In the viewDidLoad add this line, note there could be more lines in the load. This is just simplified.

- (void) viewDidLoad
{
	[self.mapView.userLocation addObserver:self 
				 			    forKeyPath:@"location" 
				                   options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 
				                   context:nil];
}

Then create the actual listing method that moves the map to the current location:

// 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];
}

Don't forget to dealloc properly and unregister the observer:

- (void)dealloc 
{
	[self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
	[self.mapView removeFromSuperview]; // release crashes app
	self.mapView = nil;
    [super dealloc];
}

Solution 3 - Iphone

Since iOS 5.0 Apple has added a new method to MKMapView. This method does exactly what you want and more.

Take a look at: https://developer.apple.com/documentation/mapkit/mkmapview

- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;

Solution 4 - Iphone

You can monitor when the MKMapView updates the user location on the map by implementing the MKMapViewDelegate protocol. Just implement :

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation      {
    CLLocationAccuracy accuracy = userLocation.location.horizontalAccuracy;
    if (accuracy ......) {
    } 
}

This callback should be perfectly in sync with what is displayed on the map.

Solution 5 - Iphone

Try this:

[mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

Solution 6 - Iphone

No problem... Inside the viewDidLoad method of your UIViewController subclass that has the MKMapView add this (assuming your MKMapView is named 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanny TuppenyView Question on Stackoverflow
Solution 1 - IphoneddnvView Answer on Stackoverflow
Solution 2 - IphoneMrHusView Answer on Stackoverflow
Solution 3 - IphonethijsaiView Answer on Stackoverflow
Solution 4 - IphoneyonelView Answer on Stackoverflow
Solution 5 - IphoneKenan KarakeciliView Answer on Stackoverflow
Solution 6 - IphoneDustin PfannenstielView Answer on Stackoverflow