iPhone Current Location Tutorial

Published:
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net.

This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn how to find the user's country code and country name, along with a numerical longitude and latitude.

Prior to doing this, add the CoreLocation framework. Make sure to import this framework as well.

#import <CoreLocation/CoreLocation.h>

Open in new window


Firstly, let's get into how to find the user's country code. Country codes can be US, CA, MX, UK, etc. The code to do this is very simple and is only 2 lines long, and it is as follows:

NSLocale *locale = [NSLocale currentLocale];
                      	NSString *countryCodes = [locale objectForKey:NSLocaleCountryCode];

Open in new window


Firstly, we create an NSLocale named Locale and we set the locale to the currentLocale. This means that we find the user's current local location. NSLocale is also used in automated localizing for the iPhone SDK. We then create an NSString called CountryCodes. This NSString is auto-released so we have to use it quickly. If you would like to have an allocated and retained NSString, you will have to assign the retain and nonatomic properties and also synthesize the string. If we add an NSLog statement, we can find the user's country code based on the NSLocale. We can set this up as follows:

NSLocale *locale = [NSLocale currentLocale];
                      	NSString *countryCodes = [locale objectForKey:NSLocaleCountryCode];
                              NSLog(@"%@",countryCodes);

Open in new window


Now, to find the country name using NSLocale, we can add this line of code after the NSString *countryCodes is created:

NSString *countryName = [locale displayNameForKey:NSLocaleCountryCode value:countryCodes];

Open in new window



This code above selects the display name from the locale's country code that we have.

Overall, it is very easy to find the user's current location. This way is used for localization and may be a somewhat invasion of a user's privacy. Therefore, if using NSLocale, we should add a UIAlertView to ask the user if we can use their current location before finding the NSLocale. Now, we will find the user's current location via a longitude and latitude. This method is great for finding the closest city or the closest location.

Firstly, we can add this code in an IBAction or a custom method:

	self.locationManager = [[[CLLocationManager alloc] init] autorelease];
                      	locationManager.delegate = self;
                      
                      	[locationManager startUpdatingLocation];

Open in new window


After doing this, add this to your .h file:

CLLocationManager *locationManager;

Open in new window


Additionally, add the properties:

@property (nonatomic, retain) CLLocationManager *locationManager;

Open in new window


@synthesize locationManager;

Open in new window


We also want to implement the delegate protocol, so add to your .h file. Mine looked like this when finished:

@interface RootViewController : UIViewController <CLLocationManagerDelegate>

Open in new window


The above code creates locationManager and initiates it. It also sets the delegate to "self" and starts updating the location. Now, we want to add a delegate method that fires when the location manager finishes updating the current location.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
                      
                      	CLLocation *location = newLocation;
                      	NSLog(@"Our current Latitude is %f",location.coordinate.latitude);
                      	NSLog(@"Our current Longitude is %f",location.coordinate.longitude);
                      
                      	float currentLatitude = location.coordinate.latitude;
                      	float currentLongitude = location.coordinate.longitude;
                      
                      	[locationManager stopUpdatingLocation];
                      
                      }

Open in new window


This code creates CLLocation from the location that we updated to. It then creates 2 floats and sets those to the corresponding longitude and latitude. We can also add an error method that fires if there is an error with the currentLocation:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
                      
                      	NSLog(@"There is an error updating the location");
                      
                      }

Open in new window


You should now know how to get your current longitude and latitude and also get the current country. I hope you have enjoyed this tutorial and thanks for reading.
2
8,726 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.