Center Maps in iOS Programming

As we follow the user on the maps. I want the blue dot (user location) to be in the center of the map. But I also want the user to be able to zoom in and out, and then after a couple of seconds to zoom in on the user location.

My educated guess for solving:. We find that the user zooms in or out, after three seconds of no scaling or detection, we begin to follow the user :). Your help will be awesome :)

This code increases the user's location, but does not delay the zoom in and out:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.0, 1500.0); [mapView setRegion:userLocation animated:YES]; } 
+7
source share
4 answers

I made a small example to show how you can delegate this task to the SDK card. Of course, you can listen to the location change, but MKUserTrackingModeFollow will automatically do this for you, so just one line of code

 #import <MapKit/MapKit.h> @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame]; //Always center the dot and zoom in to an apropriate zoom level when position changes [mapView setUserTrackingMode:MKUserTrackingModeFollow]; //don't let the user drag around the the map -> just zooming enabled [mapView setScrollEnabled:NO]; [self.view addSubview:mapView]; } 

Then the application looks like this: App with TrackingModeApp zoomed out in TrackingMode

For more information, just read the Apple documentation: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

+1
source

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]; } } 
+14
source

I had the same problem. I guessed:

  • If the user drags the map, he wants to stay in that position.
  • If the user does nothing or reset to display the current location, I must follow the user.

I added a reset button to display the current location of the user as follows: enter image description here

On the pressed reset button, change the value of needToCenterMap to TRUE

Added gesture recognizer on the map

 // Map drag handler UIPanGestureRecognizer* panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didDragMap:)]; - (void)didDragMap:(UIGestureRecognizer*)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateEnded){ NSLog(@"Map drag ended"); self.needToCenterMap = FALSE; } } 

Follow the user on the map depending on the needToCenterMap flag

 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { if (self.needToCenterMap == TRUE) [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES]; } 
+2
source

This shell does the trick: mkMapview.showsUserLocation = YES;

0
source

All Articles