Adam Storr
Adam Storr

Reputation: 1450

MapView snaps back to original location - help!

I have a mapView with custom annotations and just ran into a problem on a beta tester's iPhone. The mapView won't let the user move to any location.. as soon as you try to move, it snaps right back to the original coordinates.

Any idea why? It doesn't happen in the simulator, and I notice it a little bit on my own device... but it is a consistent problem on another device.

Thanks so much!

static BOOL haveAlreadyReceivedCoordinates = NO;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation  {
    if(haveAlreadyReceivedCoordinates) {
    return;
    }

    haveAlreadyReceivedCoordinates = YES;

    CLLocationCoordinate2D loc = [newLocation coordinate];
    [mapView setCenterCoordinate:loc];
}

Upvotes: 0

Views: 365

Answers (1)

nss
nss

Reputation: 561

Icky put you on the right track. You shouldn't be constantly resetting the map's center to whatever you get back from the location manager. The location manager will send you all sorts of updates.

You may wish to only use the update once and then ignore future updates (by setting some kind of flag), or you may wish to center the map on the user's location only when the user presses a button. In the latter case the only thing you need to do in didUpdateToLocation: is store the new location into some member variable.

The reason you don't see this in the simulator is that the location hardware simulator isn't constantly updating the way the real one is.

Upvotes: 1

Related Questions