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
}
Related
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
In Objective C, i got old location and new locations of the user
using the following method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (oldLocation == nil) return;
BOOL isStaleLocation = ([oldLocation.timestamp compare:self.startTimestamp] == NSOrderedAscending);
...
}
Now i porting the code to Swift, i cant able to get the old location from the following method
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
self.currentLocation = manager.location
}
Is there any way to get the old location or i missed anything?
There is no longer an oldLocationso you will have to rely on the timestamp of the CLLocation returned. You can check it against the current time using timeIntervalSinceNow to know if it is new or not.
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 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+.
I created many overlays in mapView with this code:
MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000];
[mapView addOverlay:circle];
I would like to remove one by one only when I execute methode:
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
My problem is: when I'm coming out of a region, do not know what I went through overlay.
Any idea how I can remove those?
Thank you.
You are going to want to monitor the regions corresponding to your circles.
Create your CLRegion:
- (id)initCircularRegionWithCenter:(CLLocationCoordinate2D)center radius:(CLLocationDistance)radius identifier:(NSString *)identifier
Monitor with CLLocationManager:
- (void)startMonitoringForRegion:(CLRegion *)region
Which will send events to your listener, so you can add/remove the overlays when you receive the events:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
Probably the easiest way for you will be to store the relationship between MKCircle objects and the corresponding CLRegion objects, so you can update your map appropriately.