Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find your current location with CoreLocation

I need to find my current location with CoreLocation, I tried multiple methods but so far my CLLocationManager has only returned 0's.. (0.000.00.000).

Here's my code (updated to work):

Imports:

#import <CoreLocation/CoreLocation.h> 

Declared:

IBOutlet CLLocationManager *locationManager; IBOutlet UILabel *latLabel; IBOutlet UILabel *longLabel; 

Functions:

- (void)getLocation { //Called when needed     latLabel.text  = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];      longLabel.text = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude]; }  - (void)viewDidLoad {     locationManager = [[CLLocationManager alloc] init];     locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move     locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m     [locationManager startUpdatingLocation]; } 
like image 719
Aleksander Azizi Avatar asked Jun 29 '11 07:06

Aleksander Azizi


People also ask

How do you get a location from CLLocationManager?

Get A User's Location Once Only once location authorization can be requested by using the CLLocationManager method requestLocation() . Implement the CLLocationManagerDelegate method locationManager(:, didUpdateLocations:) to handle the requested user location.

How can I get current location of user?

You can use the getCurrentPosition method to get the user's current location. It sends an asynchronous request to the browser, asking for consent to share their location.


2 Answers

You can find your location using CoreLocation like this:

import CoreLocation:

#import <CoreLocation/CoreLocation.h> 

Declare CLLocationManager:

CLLocationManager *locationManager; 

Initialize the locationManager in viewDidLoad and create a function that can return the current location as an NSString:

- (NSString *)deviceLocation {     return [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude]; }  - (void)viewDidLoad {     locationManager = [[CLLocationManager alloc] init];     locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move     locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m     [locationManager startUpdatingLocation]; } 

And calling the deviceLocation function will return the location as expected:

NSLog(@"%@", [self deviceLocation]); 

This is just an example. Initializing CLLocationManager without the user being ready for it isn't a good idea. And, of course, locationManager.location.coordinate can be used to get latitude and longitude at will after CLLocationManager has been initialized.

Don't forget to add the CoreLocation.framework in your project settings under the Build Phases tab (Targets->Build Phases->Link Binary).

like image 83
Aleksander Azizi Avatar answered Nov 11 '22 07:11

Aleksander Azizi


With CLLocationManager you don't necessary get the location information immediately. The GPS and other devices that obtain location information might not be initialized. They can take a while before they have any information. Instead you need to create a delegate object that responds to locationManager:didUpdateToLocation:fromLocation: and then set it as the delegate of the location manager.

see here

like image 43
ThomasW Avatar answered Nov 11 '22 05:11

ThomasW