开发者

mapView not zooming on second try using didUpdateUserLocation

开发者 https://www.devze.com 2023-03-10 09:21 出处:网络
i am loading a mapviewcontroller when the view map button is pressed from my mainviewcontroller: - (IBAction)mapButton:(id)sender {

i am loading a mapviewcontroller when the view map button is pressed from my mainviewcontroller:

- (IBAction)mapButton:(id)sender {
    MapKitDisplayViewController *mapKitDisplayView = [[MapKitDisplayViewController alloc] init];
    [self presentModalViewController:mapKitDisplayView animated:YES];   
    [mapKitDisplayView release]; mapKitDisplayView =开发者_如何转开发 nil;
}

On startup of this map view this method is called, which correctly zooms to the users location:

- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    myMapView.showsUserLocation = YES;
    NSLog(@"didUpdateUserLocation = '%@'", userLocation);

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 20000, 20000);
    [myMapView setRegion:region animated:YES];
}

I have a done button on this screen that dismisses this view:

- (IBAction)mapDoneButton:(id)sender 
{    
    [self dismissModalViewControllerAnimated:YES];
}

At this stage the user is taken back to the main view controller, however when i press the view map button again, the didUpdateUserLocation method is never called! Hence, the map view is zoomed out to the default one and wont zoom in as before.

How can i get it to be called again?

thanks


MKMapView object doesn't trigger an update on user location on its own. You need to activate the search by setting showsUserLocation = YES. This will then fetch the user's location and then the delegate method mapView:didUpdateUserLocation: is called. So set this value to YES on viewWillAppear: and once you've the user location you can set it to NO to stop tracking the user. If you don't do that, it periodically updates the user location and calls the delegate method. Without doing this, user location should be unreliable.


What i do is next:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
[[NSNotificationCenter defaultCenter] postNotificationName:@"gotUserLocation" object:self];}

then in viewDidLoad check of user location is available. if it is not register view controller as reciever for that notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(zoomToLocation) name:@"gotUserLocation" object:nil];

if user location is available just call zoomToLocation method

0

精彩评论

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

关注公众号