Why is my user location always centered?

I have a map in my application, I set the location of the user center when the map is open. I used this code:

map.delegate=Self; ...... -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { MKCoordinateRegion mapRegion; mapRegion.center = self.mapView.userLocation.coordinate; mapRegion.span = MKCoordinateSpanMake(0.5, 0.5); //Zoom distance [self.mapView setRegion:mapRegion animated: YES]; } 

But when I moved the map, it returned to the user's location. How can I freely move around the map without worrying about the user's location?

0
source share
1 answer

This is because you change the location every time the user's location changes. You have to do this only once, for example.

 -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { if (self.centerToUserLocation) { MKCoordinateRegion mapRegion; mapRegion.center = self.mapView.userLocation.coordinate; mapRegion.span = MKCoordinateSpanMake(0.5, 0.5); //Zoom distance [self.mapView setRegion:mapRegion animated: YES]; self.centerToUserLocation = NO; } } 

Where centerToUserLocation is something like @property (nonatomic) BOOL centerToUserLocation

0
source

Source: https://habr.com/ru/post/1213081/


All Articles