A quick look in the docs shows the magic.
Set userTrackingMode your map to MKUserTrackingModeFollow .
See here .
Update:
Since you updated your question, here is a new answer.
To reposition the map at the user's location, I would recommend writing a simple helper. Method:
- (void)recenterUserLocation:(BOOL)animated{ MKCoordinateSpan zoomedSpan = MKCoordinateSpanMake(1000, 1000); MKCoordinateRegion userRegion = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, zoomedSpan); [self.mapView setRegion:userRegion animated:animated]; }
And now you should call it after a short delay if the user stops moving the map. You can do this in the delegate method regionDidChange for mapView.
But you can get problems if you do not block the reset method, if the user changes the region several times before he really resets the map. Therefore, it would be wise to make a flag if you can move the map. Like a BOOL canRecenter property.
Initiate it using YES and update the recenterUserLocation method to:
- (void)recenterUserLocation:(BOOL)animated{ MKCoordinateSpan zoomedSpan = MKCoordinateSpanMake(1000, 1000); MKCoordinateRegion userRegion = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, zoomedSpan); [self.mapView setRegion:userRegion animated:animated]; self.canRecenter = YES; }
Now you can safely call him after the user has somehow moved the card with a slight delay:
- (void)mapView:(MKMapView *)mMapView regionDidChangeAnimated:(BOOL)animated{ if (self.canRecenter){ self.canRecenter = NO; [self performSelector:@selector(recenterUserLocation:) withObject:@(animated) afterDelay:3]; } }
yinkou
source share