0

I want to show the current location of the user on the map and zoom on to it.

I tried as follows:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"Init MapViewController");
        [self initMap];
    }
    return self;
}


-(void) initMap
{
    _mapView = [[MKMapView alloc] initWithFrame:self.view.frame];

    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;

    [self.view addSubview:_mapView];

    CLLocationCoordinate2D currentLocation;
    currentLocation.latitude = _mapView.userLocation.coordinate.latitude;
    currentLocation.longitude= _mapView.userLocation.coordinate.longitude;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(currentLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);

    [_mapView setRegion:viewRegion animated:YES];
}

Then in the simulator it shows a location in the sea. I set the location to London in the simulator.

modusCell
  • 13,151
  • 9
  • 53
  • 80
user1007522
  • 7,858
  • 17
  • 69
  • 113
  • 3
    Run your application in Device it will show your current location. – Bhumeshwer katre Aug 13 '14 at 09:50
  • Check this **http://stackoverflow.com/questions/20661899/cant-set-zoom-level-on-mkmapview** – Kumar KL Aug 13 '14 at 09:52
  • bhumeshwer katre is right @user1007522 – Anil Prasad Aug 13 '14 at 10:03
  • I'm now on a real device. And still shows me the same location (my viewregion is bad or ?). And I also can't zoom in or out. Even that the documentation says the zooming is by default YES. – user1007522 Aug 13 '14 at 10:11
  • When I print out the current lat and longitude they are both 0.0 0.0 – user1007522 Aug 13 '14 at 10:16
  • For the possibly 1,000th time: You cannot expect the user location to be ready to use immediately after setting showsUserLocation to YES. You must read it in the didUpdateUserLocation delegate or only use if `userLocation.location` is not `nil`. You _can_ get a simulated location in the simulator -- you don't need a device to test location services. And at least up to iOS 7, you don't have to implement CLLocationManager if you are using showsUserLocation in MKMapView. –  Aug 13 '14 at 12:46

2 Answers2

2

Just do This:

@property  (strong, nonatomic)   CLLocationManager *locationManager;

In viewdidload:

self.locationManager = [[CLLocationManager alloc] init] ;
self.locationManager.delegate = (id)self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation ;
[self.locationManager setDistanceFilter:10.0f] ;
[self.locationManager startUpdatingLocation];

And in your location manager

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
    [self.locationManager stopUpdatingLocation];
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = YourMapView.userLocation.location.coordinate.latitude;
    zoomLocation.longitude= YourMapView.userLocation.location.coordinate.longitude;
    // 2
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*1609.344, 0.5*1609.344);
    // 3
    [YourMapView setRegion:viewRegion animated:YES];
}
BHASKAR
  • 1,201
  • 1
  • 9
  • 20
0

I needed to listen to changes for the user location. Because in the view didload the location isn't known.

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

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

    [self.mapView setRegion:region animated:YES];
}

and this in the viewdidload:

[self.mapView.userLocation addObserver:self
                            forKeyPath:@"location"
                               options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
                               context:nil];
user1007522
  • 7,858
  • 17
  • 69
  • 113
  • 1
    The MKMapView has a delegate method `didUpdateUserLocation` which you should use instead of KVO. The delegate method was added in iOS 4.0. –  Aug 13 '14 at 12:47