11

I have a MKMapView and in the Map View "Show User Location" is set. The question if the app should use my location, I say yes. Then I see the blue bullet and I can zoom to the current location.

I read many other posts about this, but nothing solve the problem, that the user location won't zoom in automatically.

I want to have a zoom on startup if the user allows access the location, otherwise a defined coordinate should zoom in. (after, is the use allows the location, it can be updated, but should not set the center to the user location everytime I get updates on the location).

What are the steps to implement this behavior? I tried for example this: How do I zoom an MKMapView to the users current location without CLLocationManager? with the KVO but it does not work...

I hope someone has an idea?

Best Regards, Tim

Community
  • 1
  • 1
Tim
  • 13,228
  • 36
  • 108
  • 159
  • For iOS 5.0 and higher: [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; – Arjan Feb 26 '15 at 15:11

2 Answers2

24

Have you tried the delegate method mapView:didUpdateUserLocation:?

I used something like this in my code:

In the .h file:

@property (nonatomic, retain) CLLocation* initialLocation;

And in the .m file:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    if ( !initialLocation )
    {
        self.initialLocation = userLocation.location;

        MKCoordinateRegion region;
        region.center = mapView.userLocation.coordinate;
        region.span = MKCoordinateSpanMake(0.1, 0.1);

        region = [mapView regionThatFits:region];
        [mapView setRegion:region animated:YES];
    }
}
Joseph Lin
  • 3,324
  • 1
  • 29
  • 39
  • I have my error, it works on 4.x but not on 3.x. :-( But how can I set the coordinates to a fixed place if the userLocation is not avaliable (because no GPS or the user does not allow to use the location)? – Tim Apr 22 '11 at 17:11
  • `mapView:didUpdateUserLocation:` is available in iOS 4.0 and later, so you're out of luck if you're targeting 3.x. – Joseph Lin Apr 22 '11 at 17:20
  • 1
    I did similar things in my app, but in different order. I zoom to a default location FIRST. Then, if user allows and GPS gets a fix, zoom to user location. If not, the map stays at the default location and nothing happens. Does that make sense in your situation? – Joseph Lin Apr 22 '11 at 17:24
  • Yes, I think this is even much better than the other way round, because if user allows it, but GPS is not available (inside a building), nothing will happen. But if the user position changed, it should not go back to the user location and center it in the screen, otherwise you can not scroll to other places, because with your code, it alwys go back to the users location. Can you please provide me some code? – Tim Apr 23 '11 at 06:44
  • I edited my code so that the map view only zooms to the user location the first time. – Joseph Lin Apr 23 '11 at 20:30
2

you can do it like this in your viewDidLoad write this code

self.mapDetail.showsUserLocation = YES;
[self.mapDetail.userLocation addObserver:self
                            forKeyPath:@"location"
                               options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
                               context:nil];

and this method will do the task

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

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

    [self.mapDetail setRegion:region animated:YES];
    [self.mapDetail.userLocation removeObserver:self forKeyPath:@"location"];
}
D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30