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.
Related
I have worked on one application name time tracker. User can manually swipe in and swipe out manually by clicking the button.
Now I would like to make it as automatic based on the location detection. For that I am using CLLocationManager class. It works fine sometimes and sometimes it gives wrong swipe details. I am using below 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.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
_latitude.text = [NSString stringWithFormat:#"Latitude: %f", newLocation.coordinate.latitude];
_longitude.text = [NSString stringWithFormat:#"Longitude: %f", newLocation.coordinate.longitude];
if([_latitude.text doubleValue] > 17.76890) && [_longitude.text doubleValue] > 78.34567) {
if (isSwipeIn) {
isSwipeIn = false;
//necessary swipe out UI and logic
} else {
isSwipeIn = true;
//necessary swipe in UI and logic
}
}
}
Can anyone help me on this..
Instead of comparing lat-long, go for a range check like if your device is within few meter mark as swipe in otherwise swipe out.
You can check distance between two lat-long using following method in Objective-C
CLLocation *location; // Your Location to compare
CLLocation *currentLocation; // Your current location
double distance = [location distanceFromLocation:currentLocation]; // Returns distance in meters
// Now lets say you are within 5 meters mark Swipe In
if(distance <= 5)
// Mark swipe IN
else
// Mark swipe OUT
I hope this will help you. Happy coding :)
There is another way to do this, you can get distance from target location and check it with horizontalAccuracy of current location.
The delta gives you the distance between current location and targeted location. If delta is less than (<) horizontalAccuracy than current location is in a circle with a radius of horizontalAccuracy.
If delta is greater than (>) horizontalAccuracy than current location is far away than your targeted location.
So now CLLocationManager delegate method will be looks like below:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
_latitude.text = [NSString stringWithFormat:#"Latitude: %f", newLocation.coordinate.latitude];
_longitude.text = [NSString stringWithFormat:#"Longitude: %f", newLocation.coordinate.longitude];
// Create Location object for your target location. e.g. (17.76890,78.34567)
CLLocation *targetLocation = [[CLLocation alloc] initWithLatitude:17.76890 longitude:78.34567];
CLLocationDistance delta = [newLocation distanceFromLocation:targetLocation];
if (delta > newLocation.horizontalAccuracy) {
if (isSwipeIn) {
isSwipeIn = false;
//necessary swipe out UI and logic
} else {
isSwipeIn = true;
//necessary swipe in UI and logic
}
}
}
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/
I'm designing an app that works with Check-Ins.
GPS Permission when using the app is a must. But there's an optional feature for Automatic Check In using Geofencing, which needs the 'Always' gps permission.
I would like to ask for regular 'While Using' permissions at first. And then, only when a user wants Automatic Check In, ask for 'Always' permissions.
Is it possible?
-->First add these three keys to the info.plist file or you can add as per your requirement either 1st and 2nd key or 1st and 3rd key.
-->In "myclass.h" set up delegate as "CLLocationManagerDelegate" and it will ask you for the GPS while first time while using and the below code to the "myclass.m" file
CLLocationManager *locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestWhenInUseAuthorization]; //For while use the app
[locationManager requestAlwaysAuthorization]; // For always usage of GPS
[locationManager startUpdatingLocation];
NSLog(#"%#",[NSString stringWithFormat:#"%f",locationManager.location.coordinate.latitude] );
NSLog(#"%#",[NSString stringWithFormat:#"%f",locationManager.location.coordinate.longitude] );
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(#"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
--Note: If you want always use of GPS permission then no needs to be ask for while using in permission.
--> if you add both permission then you will get this kind of options in your device setting application and user can also set when they want to use which permissions. One permission will not show both in the device setting.
device settings --> My App --> Allow my app to access (select location) --> Location access permision
I am new in location services. I have used startMonitoringSignificantLocationChanges and (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation method but it is not getting called as I change location value. I want to fetch location value whenever user change its location.How should i achieve this? Please help me to resolve. Thanks in advance.
You need to implement "didUpdateLocations" delegate method . Here is sample
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"locations : %#",locations.description);
CLLocation *currentLocation = [locations lastObject];
NSLog(#"current location : %f %f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
}
In iOS 8 you need to do two extra things to get location working:
1. add one or both of the following keys to your Info.plist file:
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
Next you need to request authorization for the corresponding location method, WhenInUse or Background. Use one of these calls:
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
For details refer following link. http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
I'm in the process of writing an application that shows the user's distance from a fixed point as the user walks around (i.e. the label showing the distance from the user to the point is updated every time the user moves). I use a CLLocationManager with the code shown below:
- (void)viewDidLoad
{
locationManager=[[CLLocationManager alloc]init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocationDistance meters = [newLocation distanceFromLocation:fixedPoint];
self.distanceLabel.text = [[NSString alloc] initWithFormat:#"Distance: %.1f feet", meters*3.2808399];
}
The label that is supposed to show the distance from the user to the point isn't updated constantly and when it is updated, it doesn't usually show the correct distance from the user to the fixed point. I was wondering if there is a better way for me to try and do this, or do the fundamental limitations of the core location framework make this impossible. Any help will be greatly appreciated.
Are you filtering out old (cached) positions? You should also filter based on accuracy, you probably don't want low accuracy locations.
You won't get continous or periodic update, the callback only occurs when the location has changed.
Assuming the device has GPS and can see enough GPS satellites to get a good position, this works fine.
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow];
if (age > 120) return; // ignore old (cached) updates
if (newLocation.horizontalAccuracy < 0) return; // ignore invalid udpates
...
}