0

I'd like to be able to get my current user's location without actually having a map view on my view controller.

At the moment I do have a map view and get the user location by calling one of the delegate methods....

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    userlatitude = userLocation.location.coordinate.latitude;
    userlongitude = userLocation.location.coordinate.longitude;
}
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
user3626407
  • 287
  • 1
  • 6
  • 15
  • 3
    `CLLocationManager`, maybe? – holex Jul 17 '14 at 13:36
  • http://tech.pro/tutorial/971/getting-your-location-in-an-iphone-application – Sr.Richie Jul 17 '14 at 13:36
  • I vote this question up, because that's a good "newbie" question. – Mirko Brunner Jul 17 '14 at 13:44
  • @MirkoBrunner, except that searching the documentation (there is such a thing) or SO or Google would have provided the answer in 5 minutes rather than yet again posting a question that has been asked and answered hundreds/thousands of times. The question shows no research effort. –  Jul 17 '14 at 13:46
  • checkout this http://stackoverflow.com/questions/4152003/how-can-i-get-current-location-from-user-in-ios/22163556#22163556 – Mohit Jul 17 '14 at 14:30

2 Answers2

2

CLLocationManager is the class that is responsible for keeping user's location values. CLLocationManagerDelegate is another class that gets real time location data from iDevice's GPS and notifies CLLocationManager instance about the change in location and various other events, via it's delegate methods. It would be very helpful if you would read the related documentation.

You must implement CLLocationManagerDelegate protocol inside your class. You must also have CLLocationManager instance within your class that should monitor the location.

In your project, you must also add Core Location framework in Link Binaries section.

The simplest way would be:

Your .h file:

@interface MyViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocation * currentLocation;
    CLLocationManager * locationManager;
}
@end

Your .m file:

- (void) viewDidLoad
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];
}

//remember to stop before you are done, either here or in view disappearance.
- (void) dealloc
{        
    [locationManager stopUpdatingLocation];       
}

- (void)locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)locations
{
    currentLocation = (CLLocation *)[locations lastObject];
}
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
0

Easily, use CCLocationManager instead of the MKMapKit::userLocaiton method. Take a look at the "LocateMe" sample project on developer.apple.com.

Mirko Brunner
  • 2,204
  • 2
  • 21
  • 22