mapView not zooming on second try using didUpdateUserLocation

纵然是瞬间 提交于 2019-12-07 21:53:08

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6213763/mapview-not-zooming-on-second-try-using-didupdateuserlocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!