开发者

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 secon开发者_Go百科ds, 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];
}


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.


First, and I dont know that this will effect things much, but do you set your location managers accuracy and distance filter at all in viewDidLoad (or wherever you are implementing your locationManager):

[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];

Second, check these 2 functions in your source (ignore the content of the actual functions I have listed here, it is just for illustration and to show usage) and if possible, provide your code for these functions so we can better assist:

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
    [activityIndicator stopAnimating];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0)
    {
        NSLog(@"New Latitude %+.6f, Longitude %+.6f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
    }
}


I use dispatch_once in MKMapViewDelegate method to zoom only once at the beginning. Here is the code.

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation  {    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        CLLocationCoordinate2D newCoordinate = CLLocationCoordinate2DMake(mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude);

        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newCoordinate, 500, 500);
        [mapView setRegion:region animated:YES];
    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜