Keep monitoring user's location whenever it changes its position - ios

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/

Related

Location manager region monitoring

I am using following apple cllocation manager region monitoring methods:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLCircularRegion *)region
{
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLCircularRegion *)region
{
}
the problem is that these method does not get called when application is in suspended state and not running.Any help would be appreciated. thanks
Try to use significant location change instead of start updating location.Also there is a limit of radius to monitor ,minimum radius should be 100 m

For google maps iOS sdk, current location occasionally gets plotted at 0,0

Not sure why this happens but when it does we'd prefer not to show the user that their current location is 0,0 (it's in the middle of the ocean off the coast of South Africa). Is there any way to detect when this happens so we can show an error message or something to the user?
Looks like you are not authorized to access the current location of the device. For that, you'll have to add these two keys into your plist:
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
Now when you're done with with it, you will have to request for authorization like this :
if ([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)])
{
[locationManager requestWhenInUseAuthorization];
}
So now when you are done with authorization, you need to start getting location updates. For this, use this line :
[locationManager startUpdatingLocation];
Now if you wish to go to the current location, you need to implement this method here:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude zoom:currentZoom];
[self.gMapView animateToCameraPosition:camera];
}
This will keep updating the map with current location.
If you just want to current location once, just put a conditional statement for the same.
If user denies the permission, following method will be called :
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
if([CLLocationManager locationServicesEnabled])
{
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
// Put your alert here.
}
}
}

CLLocation not giving actual speed

Here is my location manager delegate code.
It is not giving speed when we move using car so at least speed value should change.
It always gives constant value -1.00.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *crnLoc = [locations lastObject];
self.speedometerCurrentValue=crnLoc.speed;
self.lblSpeed.text=[NSString stringWithFormat:#"%f",crnLoc.speed];
}
Please check this screenshot from apple explanations. Your speed is invalid.

didUpdateLocations and didUpdateToLocation

Supporting both iOS 5 and 6, one should call didUpdateLocations from didUpdateToLocation.
How do you make the call and build the Locations array? Any code sample, please?
Thank you.
You don't call those routines. The system, CoreLocation specifically, calls them.
I think this exact code is displayed in the WWDC 2012 session, Staying on Track with Location Services:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self locationManager:manager didUpdateLocations:#[newLocation]];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// your location handling code
}

ios CLLocationManager didUpdateToLocation Latitude and longitude migration

I use this code to get the longitude and latitude.
(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(#"%f",newLocation.coordinate.latitude);
self.location = newLocation;
}
But the longitude and latitude which i got has the excursion with the actual geographical position. How to solve this problem?
You should be testing the accuracy of the CLLocation object that is returned, and making sure it meets your location accuracy criteria.
if (newLocation.horizontalAccuracy > 1000) {
// Throw away this location and wait for another one as this is over 1km away
}
Also, the locationManager:didUpdateToLocation:fromLocation: method has been deprecated in iOS6 so you should use locationManager:didUpdateLocations: if you're targetting iOS6+.

Resources