iOS Programming 101: How To Get the User Location in iPhone App
This is the third article of the iOS Programming 101 series. In the previous two articles, we showed you how to hide the tab bar and implement email feature in your app. In this article, we’ll show you how to add GPS capability in your iPhone app and retrieve the user’s current location. We’ll even cover tip to translate the GPS coordinate into street address. It’s gonna to be fun and pretty easy to implement.
Location services provide a way to improve your app by enhancing the user experience. If you’re developing a travel app, you can base on the users’ current location to search for nearby restaurants or hotels. You can also find the location feature in most of the Photo apps that saves where the pictures are taken. The Core Location framework provides the necessary Objective-C interfaces for obtaining information about the user’s location. With the GPS coordinate obtained, you can make use of the API to decode the actual street or utilize the Map framework to further display the location on Map.
In this tutorial, we’ll build a simple app to show you how to use the Core Location framework. Here are the things we’ll do:
- Build a simple interface to display the GPS coordinate and address
- Learn how to use the Core Location APIs to retrieve the current location
- Utilize the built-in APIs to translate the GPS coordinates into street address
Okay, let’s get started.
Create the Project and Design the Interface
First, create a new Xcode project using the Single View Template. Let’s name the project as “MyLocationDemo” and set the project with the following parameters:
Once you’ve successfully created your project, go to the Storyboard and design the user interface. In the View, add three labels for latitude, longitude and address. For each label, place another label next to it. Later we’ll use these labels to display the GPS coordinates and address. Finally, insert a button and name it as “Get My Location”. Your user interface should be similar to the one shown below.
For the “For Address” label, change the “lines” option from 1 to 4, as we’ll display address in multi-lines. You may also decrease the font size to 14 points in the Attribute Inspector.
Establish the Connection Between Variables and UI Elements
Next, we’ll connect the UI elements with our code. In the Storyboard, select the view controller and switch to the Assistant Editor.
Press and hold the control key, click the “For Latitude Value” label and drag it towards the “MyLocationViewController.h”. Place the cursor between the @interface and @end keywords, you should see a prompt that allows you to insert an outlet.
Name the outlet as “latitudeLabel”.
Repeat the same procedures and create the outlet for the “For Longitude Value” label and “For Address” label. Finally, create an action method for the “Get My Location” button and name it as “getCurrentLocation”. This method will be invoked when it detects a Touch Up Inside event.
If you follow us correctly, the code of MyLocationViewController.h should look like below:
1 2 3 4 5 6 7 8 9 |
#import <UIKit/UIKit.h> @interface MyLocationViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel; @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel; @property (weak, nonatomic) IBOutlet UILabel *addressLabel; - (IBAction)getCurrentLocation:(id)sender; @end |
Adding Core Location Framework
To retrieve the current user location, as mentioned earlier, we’ll make use of the CoreLocation framework provided by the iOS SDK. By default, however, the Core Location Framework is not bundled in any Xcode project. We have to add it manually. In the Project Navigator, select the “MyLocationDemo” project. In the Content Area, select “MyLocationDemo” under Targets and click “Build Phases”. Expand “Link Binary with Libraries” and click the “+” button to add the CoreLocation framework.
Jump into the Code
Let’s move onto the core part of this tutorial. Core Location framework, that you’ve just added, allows you to retrieve the user’s current location in the form of latitude and longitude. It can even give you continuous location update if the user is on the move.
Like other libraries in the iOS SDK, Core Location makes use of the delegate pattern. To work with the Core Location framework, our view controller should conform to the CLLocationManagerDelegate protocol. This protocol defines methods used to receive location and heading updates from a CLLocationManager object.
To let the view controller know about the CLLocationManagerDelegate, you first import the corresponding header file. In the “MyLocationViewController.h”, add the #import statement and implement the “CLLocationManagerDelegate”.
1 2 3 |
#import <CoreLocation/CoreLocation.h> @interface MyLocationViewController : UIViewController <CLLocationManagerDelegate> |
Now go to the “MyLocationViewController.m”. Declare an instance variable and name it as locationManager.
1 2 3 |
@implementation MyLocationViewController { CLLocationManager *locationManager; } |
The CLLocationManager is the object that provides you the location data.
Add the following code in the viewDidLoad method to instantiate the CLLocationManager object:
1 2 3 4 5 6 |
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. locationManager = [[CLLocationManager alloc] init]; } |
Once you initialize a CLLocationManager object, you can simply call the startUpdatingLocation method to the location service. The service will then continuously send your application a stream of location data.
Add the following code to the getCurrentLocation method, which is hooked up the to “Get My Location” button:
1 2 3 4 5 6 |
- (IBAction)getCurrentLocation:(id)sender { locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; } |
As discussed earlier, the location data is reported to your app via the location manager’s associated delegate object. Here, we assign MyLocationViewController as the delegate object. All the location updates will send to the delegate. To capture the location event, we have to implement the delegate methods as defined in the protocol.
Add the following code to MyLocationViewController.m and place them right below the “getCurrentLocation” method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#pragma mark - CLLocationManagerDelegate - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"didFailWithError: %@", error); UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) { longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; } } |
Test Your App with Fake Location
Now you can compile the app and test it by using the Simulator. You may wonder how you can use the iPhone Simulator to test location-aware application. How can the Simulator retrieve the current location?
There is no way for the Simulator to get the current location as your computer doesn’t have GPS built-in (even you have, Xcode won’t use it). However, the Simulator allows you to fake it.
After you launch the app, click the “Get My Location” button. For the very first time you run the app, it’ll prompt the following alert to request for the access of location manager:
Remember to accept it, otherwise, the app will not be able to access the location service. Oops! The app displays an error even you tap the OK button.
Why? By default, the Simulator doesn’t know its own location. In the menu bar, select “Debug” -> “Location”. The default location is set to “None”. That’s the reason why you get the “Fail to Get Your Location” error. Now change the location setting to “Apple” (or “Apple Stores”). Your app should now show you the latitude and longitude values.
Xcode offers another way to simulate location. While running the app, you can change to other locations by using the arrow button in the top bar of the debug area.
Finding the Address
The CLGeocoder class provides services for converting between a GPS coordinate and the user-readable address of that coordinate. By specify the latitude and longitude of a given location, you can use CLGeocoder to find a user-readable address. The result (i.e. the address) returned by CLGeocoder is saved in a CLPlacemark object.
Back to your Xcode project. Add two instance variables: geocoder and placemark.
1 2 3 4 5 |
@implementation MyLocationViewController { CLLocationManager *locationManager; CLGeocoder *geocoder; CLPlacemark *placemark; } |
Initialize the “geocoder” in the “viewDidLoad” method:
1 2 3 4 5 6 7 |
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. locationManager = [[CLLocationManager alloc] init]; geocoder = [[CLGeocoder alloc] init]; } |
Update the “didUpdateToLocation” method to include the code for reverse geocoding:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) { longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; } // Reverse Geocoding NSLog(@"Resolving the Address"); [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"Found placemarks: %@, error: %@", placemarks, error); if (error == nil && [placemarks count] > 0) { placemark = [placemarks lastObject]; addressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", placemark.subThoroughfare, placemark.thoroughfare, placemark.postalCode, placemark.locality, placemark.administrativeArea, placemark.country]; } else { NSLog(@"%@", error.debugDescription); } } ]; } |
We use the “reverseGeocodeLocation” method to translate the locate data into a human-readable address. The geocoding operations doesn’t happen on the device. The method instead submits the given data to the geocoding server in the cloud in order to resolve the address. Other than the location data, you have to provide the handler that contains code to execute after the address is resolved. In this case, we’ll update the address label to display the address on screen.
The syntax of the completionHandler may be new to you. Instead of using delegate to provide feedback, the CLGeocoder uses “block” to deal with the response. By using block, you do not need to write a separate method. Just provide the code inline to execute after the geocoding call completes.
Upon completion of a geocoding request, the completionHandler will be invoked automatically. The resolved address is saved in CLPlacemark array. Placemark data includes information such as the country, state, city, and street address. So we simply pick the CLPlacemark object from the array and show the address in the address label.
That’s it. Run the app again, pick a location (e.g. Apple) and you should get its address:
Saving Battery Power
The app works but it consumes a lot of power. Why? What the app does is to retrieve the current user’s location. It shouldn’t consume much power, right?
Let’s revisit our code again. Once the user taps the “Get My Location” button, we’ll call up the “startUpdatingLocation” method to retrieve the user’s location.
1 2 3 4 5 6 |
- (IBAction)getCurrentLocation:(id)sender { locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; } |
The problem is that the method will report the location data continuously. It just keeps going and gives location update every second, even the location is unchanged. Take a look at the output of the Debug area. You should have something similar to the below:
1 2 3 4 5 6 7 8 9 10 11 12 |
2012-09-09 14:32:33.940 MyLocationDemo[13690:c07] didUpdateToLocation: <+37.33240905,-122.03051211> +/- 65.00m (speed -1.00 mps / course -1.00) @ 9/9/12 2:32:33 PM Hong Kong Time 2012-09-09 14:32:33.941 MyLocationDemo[13690:c07] Resolving the Address 2012-09-09 14:32:34.793 MyLocationDemo[13690:c07] didUpdateToLocation: <+37.33240818,-122.03049049> +/- 65.00m (speed -1.00 mps / course -1.00) @ 9/9/12 2:32:34 PM Hong Kong Time 2012-09-09 14:32:34.793 MyLocationDemo[13690:c07] Resolving the Address 2012-09-09 14:32:34.997 MyLocationDemo[13690:c07] Found placemarks: ( "Apple Inc., Apple Inc., 2 Infinite Loop, Cupertino, CA 95014-2083, United States @ <+37.33270820,-122.03033640> +/- 100.00m, region (identifier <+37.33270823,-122.03021599> radius 58.34) <+37.33270823,-122.03021599> radius 58.34m" ), error: (null) 2012-09-09 14:32:35.792 MyLocationDemo[13690:c07] didUpdateToLocation: <+37.33240773,-122.03047951> +/- 65.00m (speed -1.00 mps / course -1.00) @ 9/9/12 2:32:35 PM Hong Kong Time 2012-09-09 14:32:35.793 MyLocationDemo[13690:c07] Resolving the Address 2012-09-09 14:32:35.973 MyLocationDemo[13690:c07] Found placemarks: ( "Apple Inc., Apple Inc., 2 Infinite Loop, Cupertino, CA 95014-2083, United States @ <+37.33270820,-122.03033640> +/- 100.00m, region (identifier <+37.33270823,-122.03021599> radius 58.34) <+37.33270823,-122.03021599> radius 58.34m" ), error: (null) |
As you can see, the “didUpdateToLocation” is invoked multiple times and it just keeps going on and on.
So how can you tell the location manager to stop from updating the location? You can use the “stopUpdatingLocation” method to disable the location update. For our app, we can stop the location update once we retrieve the current location.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) { longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; } // Stop Location Manager [locationManager stopUpdatingLocation]; NSLog(@"Resolving the Address"); [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"Found placemarks: %@, error: %@", placemarks, error); if (error == nil && [placemarks count] > 0) { placemark = [placemarks lastObject]; addressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", placemark.subThoroughfare, placemark.thoroughfare, placemark.postalCode, placemark.locality, placemark.administrativeArea, placemark.country]; } else { NSLog(@"%@", error.debugDescription); } } ]; } |
Try to run the app again and check out the output message in the output area. You should notice that the “didUpdateToLocation” is called once and stopped.
What’s Coming Next?
In this tutorial, we give you a simple demo to show how to use the Core Location framework. The app is very basic. If you’re going to develop a real app, don’t forget to handle various error cases such as network error.
So what’s next? We’ll continue to work on the demo app and display the current location in a map.
As always, leave us comment and share your thought.
Comments
gezt
Authorcool tutorial thanks
Charles Leclercq
Authornothing happens when I touch the button!
Lucas Lima
AuthorThe same here, nothing happens…
d13u
Authorsetting->privacy->location toggle ON in simulator.you will find your app name just tick mark it.and then run your app
Abhishek
Authorone or two things need to be added to the info.plist and request the authorisation.
go through the link if you are using latest ios sdk.
http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
Rodrigo Rojas
AuthorThanks man, great Tutorial.
Ravi Parmar
AuthorReally great tutorial, thanks a lot.
Hamid Saleemi
AuthorCan I get the current time of the desired location from the gps coordinates ?
supaSheva
Authordid you ever get a solution to this?
David
AuthorSo cool! Thanks!!!!!
erum
Authoraka please let me know how can i find distance between two locations as i m using “old location distancefromlocation newlocation” as it is running continuously and getting current location updates but pls tell me in start old location will be 0 but after that may i need to put new location in old location or it will automatically put new location data into old location and most current location will be in new location i dnt have device i dnt know how it works in device pls help me
and how can i find speed using device w.r.t user movement
Tom
AuthorThis one shows the location and the distance from where you were previously
http://www.techotopia.com/index.php/Getting_iPhone_Location_Information_using_the_IOS_Core_Location_Framework
erum
Authoraka instead of aka sorry for typing
Malkit Singh
Authorgreat article, worth reading it….thanks…..
dilo
Authorsweet
Web Development Company
AuthorBoth the purpose property and the didUpdateToLocation method are deprecated in iOS6. It will build with didUpdateLocation but you will need to comment out the purpose assignment statement.
Tom
AuthorBeing new to ios coding…..
which part is the purpose property?
Thanks,
Tom
AuthorBeing new to all of this….
which part is the purpose assignment statement?
Thanks,
Asim
Authorhi
i want this project can you please give me link to download it??
Julien Fadel
AuthorHi, did you find the link?
Thank you, mine is not working either.
Foolish Foppish Fancyman
AuthorMissing a key piece of info. Don’t forget to add @synthesize longitudeLabel, latitudeLabel, addressLabel; in the viewController.m file.
Matthew
AuthorI didn’t think this was needed anymore since iOS 5 I think.
kushalashok
AuthorIn that case you have to use the default instance variable i.e. _latitudeLabel
Otherwise, as mentioned by Fancyman, using @synthesize is necessary.
Oliverreyes
Authorwhere do the synthesize go ?
where can i find the source code ?
Oliverreyes
Authorthe thing is when you type and try to recreate the code … it does not work, making a useless 101 tutorial
smarterkey
AuthorGreat introduction to CoreLocation. I used xcode 4.3.2 and the above tutorial worked fine with NO adjustments. (re:Oliverreyes)
Patrick CM
Authorgood tutorial. As mentioned. the three @synthesize lines need adding, other than that it worked fine. iphone 4, IOS 6.1.2 mac mini ML OS X 10.8.2 XCode 4.6
sarvjeet
Authornice1…thanks
Pranav
Authorvery useful
Thanks……
Ravikumar
AuthorThank you so much for providing this tutorial ………
npersson
AuthorAwesome as always! Really like that you are detailed and don’t leave anything out!
npersson
AuthorHaving a problem with this, I only get new coordinates EVERY OTHER TIME I click the button, not every time. What’s up with that? Anyone having the same problem?
Dalia
AuthorYou always do very good,great and clear tutorials! Thanks for sharing with us! Keep up the good work!!
Simon Ng
AuthorThanks for your support. More tutorials are coming. Keep coming back!
Roya
AuthorThank you so much for this tutorial. However, I’m running into a few issues. Can you please post the source code for this one as you’re doing for the newer tutorials?
iosNoobie
AuthorThis is awesome, thanks! Will this return the name of business nearby or just the address?
Karel
AuthorI personally was not able to get this project working on xcode 4.6.3! I was able to run the application but when I requested the location, the app halted at these lines:
longitudeLabel.text = [NSString stringWithFormat:@”%.8f”, currentLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:@”%.8f”, currentLocation.coordinate.latitude];
Any advice would be appreciated!
Commander Spock
AuthorJust put “self.” before both of those lines. Here’s how they’ll look.
self.longitudeLabel.text = [NSString stringWithFormat:@”%.8f”, currentLocation.coordinate.longitude];
self.latitudeLabel.text = [NSString stringWithFormat:@”%.8f”, currentLocation.coordinate.latitude];
Later on, you’ll run into a similar problem with a line that starts with “address.text.” Again, just add “self.” to the front of it to make it start with “self.address.text.”
The “self” prefix indicates to the class that the code is referring to an instance variable defined in the class.
Hope this helped!
showmyroutes
Authoruse _latitudeLabel.text and _longitudeLable will resolve the problem.
toqeer
Authorhi i’ve read this project and apply it but it didn’t come in delegates of CLLocationManagerDelegates, waiting for your reply
Ali Afzal
AuthorIt is the very good example for learning how to use the current location in my app.
Guest
AuthorDoesn’t work in current version of Xcode.
Simon Ng
AuthorWhich version of Xcode?
azam3d
AuthorXcode 5. I can’t retrieve the address but latitude and longitude value is fine.
Gilbert
AuthorIf the tutorial doesn’t work for you and you are getting all sorts of parse errors (like myself), try and set the deployment target to IOS 5.1 instead of 6.1…that helped for me (plus the ‘self.’ addition to the addressLabel described below).
ShanghaiTimes
AuthorBrilliant. Thanks. I struggled for days with the O’Reilly book “Geolocation for iOS” – written for an XCode4 I think. anyway, I gave up on that. Followed your guide lines, and apart from having to add the underscore “_” to _longitudeLabel.text etc, it worked right out of the box. I’m using XCode5 & iOS7
You make it so easy. Only thing I can’t get right yet, is the text in the address label. I can’t get it to wrap the lines, so that I can read it…. but hey, I have to have something to do 🙂
muthukumar
Authori got postal code and locality null. how to get postal code.
rakesh jyoti
Authoru guys are very helpful for a trainee n i am a trainee…
so a big big thank u for u guys.
E. Newton
AuthorThis was a very valuable tutorial for an Obj C beginner. You did a great job; content, presentation, format; everything!
Rasmus
AuthorVery useful, thank you! Sometimes when I run the app I get a (null). Is it possible to make it so (null) never appears?
Once again, thank you
iPhone APP Development
AuthorReally great tutorial, thanks a lot.
saccomani
Authorhow can i show a pin on user current location?!
Robin Thomson
AuthorHow to handle the deprecation
http://www.devfright.com/didupdatelocations-ios-example/
Will
AuthorNot working for Xcode 5 at all. Any tips on how it has changed?
Will
AuthorI dont quite understand the new deprecation technique.
Charles Leclercq
AuthorHi!
i’m using Xcode 6.1.1
Nothing happens when I press the button, everything seems linked…Any tips?
Dane
Authorhttps://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html
click “more” to expand the text just below the file title. I was facing the same problem, and after some digging, realized that it has to do with needing to ask permission to use the location services.
EO
AuthorI have the same problem as Charles. Can you explain what you mean by “”clicking more to expand the text just below the file title”? Thanks Dane,
Dane
Authorhttps://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/doc/uid/TP40007125-CH3-SW72
The above solved it for me. I think the format of the documentation may have changed since I last commented such that there is no more “more”
offi
AuthorHi this doesn’t work with IOS 8
u have to add entries in info.plist etc.
check this out
http://datacalculation.blogspot.in/2014/11/how-to-fix-cllocationmanager-location.html
chanduthedev
AuthorNice article and very simple and usefull. But from iOS8.0 or later, this method seems not working. Need to do below steps for iOS8.0
– Need to use didUpdateLocations delegate method instead of didUpdateToLocation
– Need to add below two key-value pairs to the info.plist file(This is very very important)
NSLocationWhenInUseUsageDescription
Your message goes here
NSLocationAlwaysUsageDescription
Your message goes here
It won’t work if you not adding the above kv pairs to the info.plist
Abhishek
AuthorGreat tutorial, very well explained and thank you. It seems in iOS 8 there are few changes in using Core Location like adding key to the info.plist and requesting the authorisation.
go through the link if you are using latest ios sdk.
http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
Darren Kressaty
AuthorHoly Schnike’s Batman! THIS is what I was missing. If you DON’T add an NSLocation…Description Key AND (&&) make an explicit call to the proper (associated) [_locationManager request…Authorization] iOS will give you the Silent Treatment like you forgot that it was Valentine’s Day! Here’s an example from my code. (I used NSLocationAlwaysUsageDescription for the purposes of the demo)
//
// ViewController.m
#import “ViewController.h”
@interface ViewController (){
CLLocationManager *locationManager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Allocate and initialize locationManager
locationManager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
}
– (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//**********
// Button Click to Start Location Services
– (IBAction)getCurrentLocation:(id)sender {
//Do some stuff after the button is clicked.
locationManager.delegate = self;
// Ask for permission to use location services.
[locationManager requestAlwaysAuthorization];
// Set the location accuracy
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Start updating the location
[locationManager startUpdatingLocation];
}
//**********
#pragma mark – CLLocationManagerDelegate
– (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@”didFailWithError: %@”, error);
/*
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@”Error” message:@”Failed to Get Your Location” delegate:nil cancelButtonTitle:@”OK” otherButtonTitles:nil];
[errorAlert show];
*/
// This fixes the deprecation of the old UIAlertView
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@”Location Alert”
message:@”Failed ot Get Your Location.”
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@”OK” style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
– (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@”didUpdateToLocation: %@”, newLocation);
CLLocation *currentLocation = newLocation;
// Note the under-bars previxing the latitudeLabel objects.
if (currentLocation != nil) {
_longitudeLabel.text = [NSString stringWithFormat:@”%.8f”, currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:@”%.8f”, currentLocation.coordinate.latitude];
}
// Stop the location updates
[locationManager stopUpdatingLocation];
// Reverse Geocoding
NSLog(@”Resolving the Address”);
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@”Found placemarks: %@, error: %@”, placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
_addressLabel.text = [NSString stringWithFormat:@”%@ %@n%@ %@n%@n%@”,
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
} else {
NSLog(@”%@”, error.debugDescription);
}
} ];
}
@end
Коля Ли
AuthorSource don’t work
Xcode 6.3 corelocation code null
Mohan C
Authorcurrent location latitude and longitude getting zero values sometime on ipad how can solve this issue? please help out.
Krishna Prajapati
AuthorThankussss
Milos Stevanovic
AuthorIs it posible to put tutorial “how to find nearby schools or stuff like that”…. connect to gmaps or something like that and use it to find specific places?
raj
AuthorI want source code any one can help me
my mail id :[email protected]
Julien Fadel
AuthorHi man, Where is the other tutorial with the map? the thing coming next…
ShanghaiTimes
AuthorIs there an updated version of this for XCode 8, iOS 10. Which doe3s location services differently?
Mani Kashyap
AuthorTruly helpful 🙂 Thanks.
Seung Soo Fredy Han
Authordon’t you have swift tutorial for this ‘location app’?