简体   繁体   中英

iOS 8 - Map keeps reanimating back to user location

this question has been stumping me for a while: my map, when I run it on my iOS device, has started to 'boomerang' back to the user's location whenever I scroll to look at a different pin I recently dropped. It, strangely, has only begun doing this recently.

Here's my code for that:

- (void)viewDidLoad
{
   [super viewDidLoad];
   self.searchBar.delegate = self;
   self.locationManager = [[CLLocationManager alloc] init];
   self.locationManager.delegate = self;

   if ([self.locationManager respondsToSelector:@selector
     (requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
     mapView.showsUserLocation = YES;
   }
   [self.locationManager startUpdatingLocation];
}

#pragma mark cllocation delegate methods
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:
(NSArray *)locations
{
NSLog(@"location info object=%@",[locations lastObject]);
[self.locationManager stopUpdatingLocation];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Success"   
 message:@"Location      
received" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}

Any ideas on how I can handle the user location problem?

Your code says:

 mapView.showsUserLocation = YES;

That means that map view is tracking the user location. It wants to keep showing it. If you want the map view to stop doing that, stop tracking the user location.

Typically, you'll adjust the MKUserTrackingMode. The simplest way to do that , and to let the user do it, is to provide an MKUserTrackingBarButtonItem.

Also, make sure that you are not setting the map region somewhere else. Setting the map region can fight against scrolling the map if you're not careful.

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