iOS 6 CLLocationManager last location - ios

So, as of iOS 6, -locationManager:didUpdateToLocation:fromLocation: is deprecated.
Apple suggests, instead, using -locationManager:didUpdateLocations:, which provides anywhere from one to a series of recent location changes. However, in the incredibly likely chance it provides a locations array of length 1, there appears to be no way to access the fromLocation:(CLLocation *)oldLocation parameter of old.
Is there a way to get at this information without using deprecated methods?

You just need to set a property to the value that locationManager:didUpdateLocations: returns, which you can use as the fromLocation, and then call the method again, and use what it returns as the toLocation.

As rdelmar said, if you need the previous location, I would just store the last location received from locationManager:didUpdateLocations: in an ivar or property, and if locationManager:didUpdateLocations: returns an array with only one value, grab the value you previously saved in your ivar/property and use that for your "old" location.

Related

Swift: How to manually call CLLocation

So normally when you try to grab a user's location using CLLocation, you'll use the didUpdateLocations method which(if everything has gone well) will be called when using locationManager.startUpdatingLocations().
But I need to be able to manually call the function that gets the location so I can use a completion handler with it. How would I be able to get the user's location after start updating locations without using the didUpdateLocations method?
You can't "manually call CLLocation". After you start updating locations, the system begins calling your didUpdateLocations method. The first locations you get may have pretty poor accuracy. To get a good location you really need to check the accuracy and wait until you get a location that meets your requirements before you accept it.
I haven't used the new requestLocation method yet, so I don't know how it acts with regard to the poor results you get when you first request locations. The docs say "The location fix is obtained at the accuracy level indicated by the desiredAccuracy property" but supposedly startUpdatingLocations also honors the desired accuracy and the first few readings you get from it tend to be quite bad if the GPS was off when you called startUpdatingLocations.
In any case, both approaches call didUpdateLocations with the result/results.
If you want to invoke a closure/completion handler once you obtain a location then I suggest you create your own location manager singleton. Let's call it MyLocationManager.
You could give it a method getLocation(completion:) and have it take a completion block. The implementation would either start location updates or call requestLocation (if that method gives back locations that are dependably good the first time). It would also save away the completion handler closure in an (optional) instance variable. In MyLocationManager's implementation of didUpdateLocations it would check for a completion handler and call it if one was found, and then nil out the completion handler.
You can access the CLLocationManager's location property at any time. This will have the most recently determined location. It may be nil if no location has been determined.
If you are targeting iOS 9 & later then you can use the requestLocation method to request a single location update. The location update will be delivered to didUpdateLocations as usual. There is no synchronous/blocking "request location, wait for it to update and return the location to me" method.

CLLocationManager location update callBack on ios9

I have developer an walk app that can record distance, on ios7,ios8,the distance almost fine although some time not accurate,but on ios9.0, the distance often too larger, so I check
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)
Locations method, and find some time timestamp of new location early than some previous location, it means location from callBack are not sorted by timestamp, why is that and how deal with it,
Thanks in advance
after many try, I found call startUpdatingLocation in a period timer cause CLLocation returned by delegate often wrong, I think that's the problem

Getting user's current location before doing next event in single method?

My requirement here is to get the user's current location and pass to map.google.com. And the most imp condition is : these two events must happen in a single method.
And my problem here is , I have to pass the user's location, but it is only obtained through the delegate of CLlocationManager.Basically i need user's location before calling for map.google.com.
(imp: i can't call for CLLocationManager in my viewwillappear or anywhere else in the code)
So, can we solve this issue using block handler?
Any help,links would be appreciated.
One of several solutions could be to use a third party library. Here one I used for geolocation, very light, simple, and blockbased : https://gist.github.com/steakknife/6972228
EDIT :
Here's an example to easily find the current location in one-line way using the synchronous method currentLocationByWaitingUpToMilliseconds:success: of the little library quoted above :
CLLocation *location = [LocationManager currentLocationByWaitingUpToMilliseconds:1000 success:nil];

Is it possible to store subclasses of CLRegion with startMonitoringForRegion: and retrieve a correct version/copy of it later with monitoredRegions

This week I tried to subclass CLRegion class by adding new properties and methods on it and then monitor it.
#interface MyClass : CLRegion
I did this because I found it way cleaner than storing separately the CLRegion, and the added properties separately.
I implemented the NSSecureCoding and NSCopying protocol in this subclass to make it work.
Everything went fine for monitoring this subclass with startMonitoringForRegion: or stopMonitoringForRegion:.
But when retrieving existing regions with (NSArray *)monitoredRegions: my problem occurs:
If I identify the region as a subclassed Region by the identifier,
and then cast it to the subclassed class (MyClass *) to call specific methods,
this doesn't work and I get a "unknown selector for CLCircularRegion" exception.
I highly suspect that this doesn't work because there is no such thing as virtual methods in Objective-C and so the son's init//initWithCoder//encodeWithCoder//.. is not called when the objects are builded by monitoredRegions.
As for know, i'm forced to store the additionals properties in NSUserDefaults and retrieve it when I identify the region, but I find it a little bit dirty...
So is there a way to do it like this ? Or should I keep the NSUserDefault solution ?
no you will get 'stock' CLRegions
quote: "The objects in this set may not necessarily be the same objects you specified at registration time. Only the region data itself is maintained by the system. Therefore, the only way to uniquely identify a registered region is using its identifier property."

iOS CLLocationManager - How to call a method when the location is determined

I am using CLLocation manager to get my lat & long, and it seems to work rather well. However, the first reading of accuracy no matter what, is always 10. From here it seems to home-in correctly starting at about 1500m. The reason that this is a problem is because to determine whether the method is called, I am checking if the accuracy is below 15, which this false reading is, and therefore calling the method at the wrong time. How do I get around this?
CoreLocation may cache some old location values to "speed up" first location update. You can see if your location is a new one by checking CLLocation's timeStamp property.
you can check using horizontal accuracy there are many posts in stackoverflow regarding cllocationmanager. And for calling a method you can use NSNotificationCenter,Deleagate & protocol mechanism and IBAction of course. It depends on your requirement.

Resources