简体   繁体   中英

iOS6 iPhone App — How to zoom to userLocation only once?

The below code is the method I've been using to zoom into the user's location, but then stop it from continuously re-zooming to it once the user moves the map. The problem with this method is that when it asks permission for userlocation data in the app, and I select yes, it doesn't do anything. I have to leave the page and return before it zooms correctly.

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([self.mapView showsUserLocation])
    {
        MKCoordinateRegion region;
        region.center = self.mapView.userLocation.coordinate;

        MKCoordinateSpan span;
        span.latitudeDelta  = .50; // Change these values to change the zoom
        span.longitudeDelta = .50;
        region.span = span;

        [self.mapView setRegion:region animated:YES];

        self.mapView.showsUserLocation = NO;}
}

The above method should be monitoring for user location changes and then set the location once it's found. I then set a condition in the if statement to make it stop -- otherwise it keeps moving back to the userlocation when you move the map around.

It sounds to me like self.mapView.showsUserLocation is set to FALSE when the popup appears because it doesn't know the user's location yet. To get around this problem, I would advise just using your own variable. Create a global boolean in this class and use it instead of self.mapView.showsUserLocation. Also, make sure that this function is, in fact, being called after the user agrees to the popup. Have you been NSLogging?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM