Objective c gps coordinates not working - ios

I have followed many tutorials but none have worked. The coordinate always outputs 0.000000, 0.000000.
Here is my code for the location:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
currentLocation = (CLLocation *)[locations lastObject];
}
-(void)getLocation {
longitude = floorf(currentLocation.coordinate.longitude * 10000)/10000;
}
I have this in a button:
locationManager stopUpdatingLocation];
[self getLocation];
And I have this in the viewDidLoad:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
I am using a fake location in the ios simulator in xcode so it should not output 0.000000.

You need to request authorization to use location services.
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
[self.locationManager requestAlwaysAuthorization];
}

You may need to add this line in viewDidLoad:
[locationManager requestAlwaysAuthorization];
If that doesn't work, I'm inclined to say it's because you're running in the simulator. Even the fake location was screwing me up at one point. Can you run on a device?
Edit: try adding this to info.plist:
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>

follow this tutorial you found the lat & long and by using both get the address also...:)
https://www.appcoda.com/how-to-get-current-location-iphone-user/

Related

CLLocationManager updates location with inaccurate coordinates

My code is for a Golf player app. I need the exact user location here is my present code
- (void) update_location
{
locationManager_player = [[CLLocationManager alloc] init];
locationManager_player.delegate = self;
locationManager_player.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
[locationManager_player requestWhenInUseAuthorization];
}
[locationManager_player startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// Code to handle the current user location
}
The above delegate method is not giving the exact user location. Perhaps this gives the coordinates which are 30 to 40 yards far from user current position
Thanks in advance
Location accuracy is controlled by the desiredAccuracy property of CLLocationManager. Check what is set in your case. Default value is kCLLocationAccuracyBest. You can set it to kCLLocationAccuracyBestForNavigation but it require more power requirements.
More detail here.

How to get location on each 200 meters in terminated state in ios

i am trying to get user's location in app's terminated state. i am doing this by startMonitoringSignificantLocationChanges but it's giving location after 3km or after 5 min. so can't create a route properly. please guide me how to do this.
if (_anotherLocationManager)
[_anotherLocationManager stopMonitoringSignificantLocationChanges];
self.anotherLocationManager = [[CLLocationManager alloc]init];
_anotherLocationManager.delegate = self;
_anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
if(IS_OS_8_OR_LATER) {
[_anotherLocationManager requestAlwaysAuthorization];
}
[_anotherLocationManager startMonitoringSignificantLocationChanges];
I solved my query myself...
Create a locationManager object and alloc it like this
self.locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // setting the accuracy
[self.locationManager requestAlwaysAuthorization];
if([self.locationManager respondsToSelector:#selector(allowsBackgroundLocationUpdates)]) {
[self.locationManager setAllowsBackgroundLocationUpdates: YES];
}
self.locationManager.distanceFilter = 20.0;
[self.locationManager startMonitoringSignificantLocationChanges];
self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;
[self.locationManager startUpdatingLocation];
self.locationManager.pausesLocationUpdatesAutomatically = YES;
self.locationManager.delegate = self;
Now set location manager delegate method.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
{
if (newLocation.speed>7){
// here you got location, i settled speed 7 for my convenience.
}
if (newLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy) {
//Desired location Found
[self.locationManager stopUpdatingLocation ];
}
}
You must have to write this stopUpdatingLocation, else your battery consumption will increase so high.
Use the Geofencing to achieve the same.
Create a Geofence of 200mtr radius.
By default, notifyOnExit would be true
Implement the delegate didExitRegion of LocationManager
For termination state, the app would be lunched with UIApplication.LaunchOptionsKey.location in didFinishLaunchingWithOptions.
Create an instance of Location Manager object on location launch key, you obtain the region at which the location exited and keep creating the 200mtr fence on every exit.
You're not going to get 200 meter granularity or continuous tracking with significant location monitoring.
Have you seen these notes in the docs:
The significant-change location service delivers updates only when
there has been a significant change in the device’s location, such as
500 meters or more.
If GPS-level accuracy isn’t critical for your app and you don’t need
continuous tracking, you can use the significant-change location
service.

Using freeway drive/ city run in iOS simulator in background mode

I am trying to make use of the options within iOS simulator : debug->freeway drive/ city run in order to simulate the location updates.
In my code I am using CLLocationManager for getting location updates with following code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDistanceFilter:20];
}
-(void)viewWillAppear:(BOOL)animated {
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)lm didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
NSLog(#"Location returned: %f, %f Accuracy: %f", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
}
I am never getting a callback on the delegate for location updates, while my app is in background and i am selecting the option in simulator.
I have provided my app the background mode for location updates. Please let me know how exactly to use these features or if i am missing anything here.
I finally sorted out the problem. The simulator's options are working perfectly fine but it was the implementation of CLLocation which was the problem.
On iOS 8 the location update code will not work unless :
You add NSLocationWhenInUseUsageDescription & NSLocationAlwaysUsageDescription to the plist with some string values that will be prompted to user.
You need to add ask user's permission for getting the location codes to work:
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
Taken from this post.

CLLocation won't start updating location

I am having a lot of trouble after updating to iOS 8. The following code worked before I updated to iOS and Xcode 6. Code is written inside viewDidLoad:
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
In my .h file, I have locationManager as a property, also declaring CLLocationManagerDelegate:
#property CLLocationManager *locationManager;
I've set a breakpoint inside
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
but the code never ran.
What step am I missing???
EDIT: do note that the prompt asking the user to allow location services never appeared. I suspect that this is the issue but I did request the service right after I declared the delegate to self.
Also added
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your message goes here</string>
But still does not work for me
if you start location before user agree to use location server
ios8 or later change to
- (xx)xxxx
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) {
[self.locationManager startUpdatingLocation];
}else{
[self.locationManager requestWhenInUseAuthorization];
}
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined){
[self.locationManager startUpdatingLocation];
}
}
You need to add NSLocationWhenInUseUsageDescription key to your info.plist. (From my testing, it can be an empty string, but the key must exist. Not sure if Apple fails review for empty string, though.)
You need to use requestAlwaysAuthorization (for background location) or requestWhenInUseAuthorization (location only when foreground) call on CLLocationManager is needed before starting location updates.
There also needs to be NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist with a message to be displayed in the prompt.
More information in this post: Location Services not working in iOS 8
So after all the "iOS 8" issues, I realized my problem was that I didn't allocate and init my locationManager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) {
[self.locationManager startUpdatingLocation];
}else{
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];

CLLocation didupdatetolocation not called

Spent few days trying to solve this but unable to find any solutions that works. I have checked all the post on stackoverflow and tried all their solutions and nothing seems to be working for. I have also tried the apple test project for CLLocation which works fine for me. I used bits and pieces from the apple test project
https://developer.apple.com/library/ios/samplecode/LocateMe/Listings/README_md.html
But my code is not working at all. The DidupdateToLocation never gets called.
Here is my code (in viewDidLoad)
_locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// This is the most important property to set for the manager. It ultimately determines how the manager will
// attempt to acquire location and thus, the amount of power that will be consumed.
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Once configured, the location manager must be "started"
//
// for iOS 8, specific user level permission is required,
// "when-in-use" authorization grants access to the user's location
//
// important: be sure to include NSLocationWhenInUseUsageDescription along with its
// explanation string in your Info.plist or startUpdatingLocation will not work.
//
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
[self performSelector:#selector(stopUpdatingLocationWithMessage:)
withObject:#"Timed Out"
afterDelay:30];
I have checked to make sure locationServicesEnabled is enabled.
I have added NSLoationWhenInUseUsageDescription property to the info.plist file. Is there any other property I need to add or enable any services??
I cannot for the love of god figure out what i have done wrong. Can some one please help me with this.
You have to call the startUpdatingLocation after you received the didChangeAuthorizationStatus callback on iOS8.
//iOS 8 API change
if([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[self.locationManager requestWhenInUseAuthorization];
}else{
[self.locationManager startUpdatingLocation];
}
implement this delegate method:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusDenied:
{
// do some error handling
}
break;
default:{
[self.locationManager startUpdatingLocation];
}
break;
}
}
First, don't forget, with IOS 8 you need to do [locationManager requestWhenInUseAuthorization] plus [locationManager requestAlwaysAuthorization];
_locationManager = [CLLocationManager new];
if(SYSTEM_IS_OS_8_OR_LATER) {
[_locationManager requestWhenInUseAuthorization];
[_locationManager requestAlwaysAuthorization];
}
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//and the problem with your code : don't forget to start
_locationManager startUpdatingLocation];
and in your didUpdateLocations
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
_currentLocation = [locations objectAtIndex:0];
//do your stuff with the location
}
Silly me. I added the keywords to the wrong plist file. I have got it working now. Thanks for your help guys.
I just spend an entire morning on this [in honestly I am relative new OOC], anyway the problem..
didUpdateLocation compiles and is never called
didUpdateLocations compiles and does work!!
Not the answer in this case it would seem, but it is in the question, an easy mistake to make. Using IOS 8.1.3 & Xcode 6.1.1
Simply add three properties in Info.plist.
To get the current location, if it is not calling didUpdateToLocation method
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
privacy - location usage description of type String.

Resources