CLLocation: Blue current location dot moves unexpectedly - ios

This is my code in viewDidLoad:
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
if ([[UIDevice currentDevice].systemVersion hasPrefix:#"8"]) {
[_locationManager requestAlwaysAuthorization];
}
[_locationManager startMonitoringSignificantLocationChanges];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[_locationManager setDistanceFilter:50];
[_locationManager startUpdatingLocation];
[_locationManager startUpdatingHeading];
This is how my didUpdateLocations method looks like:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* newLocation = [locations lastObject];
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 50.0 && newLocation.horizontalAccuracy > 0) {
// If the event is recent, do something with it.
NSLog(#"latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude);
}
}
The problem is that the blue current location dot moves around inaccurately and doesn't stay still.I observe this behavior when I am stationary and not moving around. I am trying to filter the locations in the didUpdatelocations method but I don't know why it is not working. Please guide me as to how should I go about filtering it. Or do you think this wrong behavior is because of something else?

Have you tried reducing setDistanceFilter:50? Per the framework reference, this means that the location only updates after the device has moved 50 meters. The default value is kCLDistanceFilterNone to be notified of all movements.
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instp/CLLocationManager/distanceFilter

Related

Offline Location Update is not working in iOS 14 and above

I am working on the Location update in my application
When the app is with internet connection location update (didUpdateLocation) is called. But when the app goes offline, Location update delegate is not called in iOS 14 . I am not getting the latitude and longitude.
Any idea why this is happening or is this the process of location manager?
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
if([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}else{
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
lattitude = [NSString stringWithFormat:#"%f", location.coordinate.latitude] ;
longitude = [NSString stringWithFormat:#"%f",location.coordinate.longitude];
[locationManager stopUpdatingLocation];
}

iOS GPS Location Tracking

I am programming in Objective C on Xcode and seem to be having a problem when my app goes into the background. I have a GPS tracking app and it works fine when the user has the app running but when it goes in the background the GPS doesn't follow the user location step by step. It pauses and then when the app re-opens, it automatically finds the location but I want the GPS to be running all the time so it won't "go off road"1 like I have in the picture attached.
I have location updates and background fetch turned on already in background modes.
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations: (NSArray *)locations
{
if (!self.timerPause) {
for (CLLocation *newLocation in locations) {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (fabs(howRecent) < 10.0 && newLocation.horizontalAccuracy < 20)
other code
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
self.locationManager.distanceFilter = 10; // meters
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
locationManager.allowsBackgroundLocationUpdates = YES;
Add this where you have set up the location manager. It should work :)

CLLocationManager Repeating Few Times

Having a bit of an issue. I have a GMSMapView in my application and it points to the users location upon loading. When the app loads I have the locationManager set to display the latitude in the log once it finds the users position. Right now it displays the latitude over 3 times (I plan to make this post to my server). When I launch my application I will have quite a few users and the extra 3 server requests per user would be a lot for a server to handle. I am wondering is there anything I have done wrong in the code or is there a way to get around this? Will attach the code below.
Thanks!
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:(id)self];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self.locationManager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
longitude:newLocation.coordinate.longitude
zoom:17.0];
[self.mapView animateToCameraPosition:camera];
latitude = newLocation.coordinate.latitude;
longitude = newLocation.coordinate.longitude;
NSLog(#"latitude = %g", latitude);
}
didUpdateToLocation was deprecated way back in iOS6. Use the replacement didUpdateLocations, and then just pick off the last element of the array e.g.:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
location = [locations lastObject];
// ...
latitude = location.coordinate.latitude;
longitude = location.coordinate.longitude;

CLLocationCoordinate2D latitude & longitude always zero

I am trying to get latitude & longitude. I am running following code on actual device but I always get zero for latitude & longitude. I also import library and set delegate also. May I know what is wrong and how to do? My device has ios 8.
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager locationServicesEnabled])
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *str=[[NSString alloc] initWithFormat:#" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(#"%#",str);
you have to get the current location in delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations objectAtIndex:0];
}

Can't enable the location in my app

I want to find my location in my app. And I use CoreLocationManager to get the coordinate, but when I launched the app,it will alert me weather to authorize the location privacy, and after I click the OK button, nothing happened.
Then I open the Settings-Privacy-location in my iPhone, I found my app had not get the location enabled. I turn it on, then I relaunch the app, still nothing happen?
Here is my code:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
}
the method -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations can't run after I set the breakpoint.
Put the following code to get your current latitude and longitude in yor device (Not in simulator).
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
latitude = [[NSString alloc] initWithFormat:#"%f", coordinate.latitude];
longitude = [[NSString alloc] initWithFormat:#"%f", coordinate.longitude];
NSLog(#"dLatitude : %#", latitude);
NSLog(#"dLongitude : %#",longitude);

Resources