Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing MKMapView from continually re-zooming and re-centering to user location

I seem to have the opposite problem from many of those posted here about MKMapView. Rather than not being able to get it to zoom to and display the current location, I can't seem to get it to stop doing so. Here's what happens:

  • I launch the app
  • The MKMapView shows my location with a blue dot
  • I zoom out and away on the map using my fingers
  • After a few seconds, the MKMapView suddenly zooms back in to center on my current location again

I've tried telling my CLLocationManager to stopUpdatingLocation (no effect, since an MKMapView knows how to use CoreLocation), and I've tried telling the MKMapView to setShowsUserLocation:NO (no blue dot displayed at all, which is not what I want). I even tried eliminating my CLLocationManager (no effect). What is causing this and how do I stop it?


Yes, I do set the location manager's accuracy and distance in -loadView.

I don't implement -mapViewDidFinishLoadingMap:. Here is my implementation of

-locationManager:didUpdateToLocation:fromLocation:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // How many seconds ago was this new location created?
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];

    // CLLocationManagers will return the last found location of the device first,
    // you don't want that data in this case. If this location was made more than 3
    // minutes ago, ignore it.
    if (t < -180)
    {
        // This is cached data, you don't want it, keep looking
        return;
    }

    [locationManager stopUpdatingLocation];
}

I think this is the centering you're asking about, @Anna Karenina:

- (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)u
{
    CLLocationCoordinate2D loc = [u coordinate];
    // Display the region 500 meters square around the current location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
    [mv setRegion:region animated:YES];
}
like image 521
DJHoward Avatar asked Sep 23 '11 02:09

DJHoward


1 Answers

Dont implement - (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)u method.

Instead use locationmanager didUpdateToLocation.

didUpdateUserLocation is called multiple times without any reason.where didUpdateToLocation is called when any location changes occurred.

Then u can manually set the mkmapview region in the didUpdateToLocation method.

like image 122
tasnim ahmed Avatar answered Nov 09 '22 20:11

tasnim ahmed