iOS MKMapView - stop map view from re-centering to the user's location if the user has panned/dragged the map view

StackOverflow https://stackoverflow.com/questions/21598632

Question

I have an MKMapView in which I have set:

[self.mapView setShowsUserLocation:YES];

I want to show the user's location, and update the location if he/she moves. When didUpdateUserLocation gets called it seems like the map re-centers to the user's location, even if the user have panned the app to see another region. What I want is to be able to track the user's position, but also let the user explore the map. My didUpdateUserLocation currently looks like this:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    _currentLocation = userLocation;

    [self.mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(self.currentLocation.coordinate.latitude + 0.0015, self.currentLocation.coordinate.longitude), MKCoordinateSpanMake(0.01, 0.01)) animated:NO];

    [...]
}

Any ideas on what I should do to achieve what I want?

Was it helpful?

Solution

As long as you set the showsUserLocation property of your MKMapView as YES, the user location is automatically updated (cf Apple Docs).

You should remove the "setRegion" line, because this line centers the view on the user location.

OTHER TIPS

I know the post is long gone, but I'd like to share what worked for me while searching for answers to so many similar questions. Many of them didn't work at all. I wanted to move my location to one side while i emailed a screenshot of Annotations, otherwise I would have to zoom out so that the area under my pins only occupied exactly a quarter of the available area.

- (IBAction)beStill:(UIButton *)sender {
_mapView.showsUserLocation = NO;
}

- (IBAction)moveOn:(UIButton *)sender {
_mapView.showsUserLocation = YES;
}

While i was still sorting it out I put two buttons on screen, but then refactored the UI so that whatever was the last control the user touched was also safe to infer whether he wanted it ON or OFF. Then i just slipped the statements into those respective methods, and deleted the beStill and moveOn buttons.

This is important because when you launch the app you first want the function ON, unless you're in the Atlantic off the west coast of Africa

That way i didn't have to do anything with setRegion

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top