1

I'm setting region coordinates here:

// Center Map Here on initial load
#define CENTER_LATITUDE 22.11111  #fake for example
#define CENTER_LONGITUDE -23.99292 #fake for example

I'm setting the region zoom here:

MKCoordinateRegion region;
region.center.latitude = CENTER_LATITUDE;
region.center.longitude = CENTER_LONGITUDE;
region.span.latitudeDelta = SPAN_VALUE;
region.span.longitudeDelta = SPAN_VALUE;
[self.mapView setRegion:region animated:NO];

Here is my mapView didUpdateUserLocation method:

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation 
  *)userLocation {

  CLLocationCoordinate2D loc = [userLocation coordinate];
  MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
  [self.mapView setRegion:region animated:YES];

  self.mapView.centerCoordinate = userLocation.location.coordinate;

}

I know this will only zoom in on the defined region, but using something similar to this, is there a way to get the current user location and not a set predefined coordinate like the ones above?

I want the map to find current user location and then zoom to it on the map, if that makes sense.

Michael Sebastian
  • 785
  • 3
  • 15
  • 33
  • 1
    See [Getting User's Location](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW10) in _Location and Maps Programming Guide._ – Rob Jan 17 '16 at 07:30

2 Answers2

1
#import <CoreLocation/CoreLocation.h>

Remember to include the LocationManager delegate within the class file

@interface YourViewControllerClass ()<CLLocationManagerDelegate>

@property (strong, nonatomic)  CLLocationManager *locationManager;

@end

Set up your locationManager when you are ready to search for the location - from within an IBAction method or even viewDidAppear etc.

-(void)startUserLocationSearch{

     self.locationManager = [[CLLocationManager alloc]init];
     self.locationManager.delegate = self;
     self.locationManager.distanceFilter = kCLDistanceFilterNone;
     self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    //Remember your pList needs to be configured to include the location persmission - adding the display message  (NSLocationWhenInUseUsageDescription)

     if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
         [self.locationManager requestWhenInUseAuthorization];
     }
     [self.locationManager startUpdatingLocation];
}

Then this delegate method will fire once the location has been retrieved.

 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

      [self.locationManager stopUpdatingLocation];
      CGFLoat usersLatitude = self.locationManager.location.coordinate.latitude;
      CGFloat usersLongidute = self.locationManager.location.coordinate.longitude;

      //Now you have your user's co-oridinates
}

I hope this helps.

Jim Tierney
  • 4,078
  • 3
  • 27
  • 48
  • Thanks. What do you mean by setting up the locatioManager within an IBAction method? – Michael Sebastian Jan 17 '16 at 18:44
  • Hey Jim =- I have it working, but it keeps recentering back to the user location? Why is that? – Michael Sebastian Jan 17 '16 at 19:35
  • Hi @MichaelSebastian where are you calling the startUserLocationSearch from? It sounds like you're calling this again. If you only need location once, then try setting it up to call from a button action, as once it hits the delegate method the 'stopUpdatingLocation' instruction stops it searching again (until the start search method is requested again) – Jim Tierney Jan 17 '16 at 19:42
0

Add these two lines in the didUpdateLocation method:

CGFLoat CurrentLatitude = self.locationManager.location.coordinate.latitude;
CGFloat CurrentLongitude = self.locationManager.location.coordinate.longitude;
Pang
  • 9,564
  • 146
  • 81
  • 122
Pradnya
  • 7
  • 1
  • This is pretty much just repeating what [the existing answer](http://stackoverflow.com/a/34836462) has already said. – Pang Feb 24 '17 at 06:55