I am using CLLocationManager didUpdateLocations to get current location of device.
Its provide me the current location but after in few seconds I am getting different latitude and longitude for the same place with no movement in device.
I want to implement 5 metre distance check in my app, but due to frequently change in location provided by delegate method I always getting the more than 5 meter difference between old and new location.
Can anybody suggest what is the best way to find out accurate distance.
Related
For an app I am making I track the users location on a map. When outdoors, I am able to get the coordinates of the users position almost instantly and the app works great. Indoors, as expected, does not have GPS signal so I cannot get the location. That said, iOS uses Wifi and various other techniques to show my approximate location on a MapView still. However, I am unable to get the approximate coordinate while indoors even though iOS knows it (ie. didUpdateLocations is never called while indoors but is the second I walk out the door). I'm wondering if there is a way to get an approximate coordinate while indoors (I don't need exact).
I set the location accuracy with this setting:
locationManager.desiredAccuracy = kCLLocationAccuracyBest
And I get the location with this:
if let location = locations.first {
....
}
But it updates location several times and many of them are not accurate. For example when I find the location, it's about 100 meter away from the device location.
How do I get an accurate location?
I know that the first few locations are not accurate until the device gets a fix on the location.
From the documentation:
The receiver does its best to achieve the requested accuracy; however,
the actual accuracy is not guaranteed.
Because of this, you can't guarantee how close the detected locations are, but there are a couple of other things that you could try:
Use kCLLocationAccuracyBestForNavigation. This should get you a more accurate reading, but with the cost of more battery usage.
Filter the locations, though I don't know if this is even possible, because the only data you have is what was received from the location manager that you are using.
From experience over the years with core location. It takes sometime for the GPS chip to "Warm up" and sometimes reports several locations before getting a lock on. You will experience this more indoors rather than with clear view of the sky. Buildings with metal roofs also effect the accuracy. You can however ignore gps updates if they are in/out of a certain threshold using distanceFilter property (Specified in meters) distanceFilter and setting desired accuracy using this property also horizontalAccuracy.
You can check accuracy for every update you get, and filter locations until they get to reliable accuracy:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(manager.location!.horizontalAccuracy)
if locations.last!.horizontalAccuracy <= manager.desiredAccuracy{
print(manager.location)
}
}
Take a look at the horizontalAccuracy property of the location (CLLocation). This is measured in meters. You can use this property to disregard location readings which are not accurate enough.
I am using a CLLocationManager to get the location data in my ios app, I am using locationManager.distanceFilter method to update how often I want to update my location.
Does this method record the location every time the device moves at least that much of distance? and what happens if it is set to 0? will it update the location even if the location is not changing?
Any help would be appreciated.
With the distance filter set to 0 you should get new location about every 1 second. All of them will be slightly different even if you are not moving because GPS/GLONASS accuracy is not perfect. If you set it to some bigger value next location will be reported only when the distance between locations will be greater than specified value.
what's the efficient way to keep track user's location? I mean I do not care if the location has been change slightly, because it will waste the resources.
i used in my code: LocationManager.stopUpdatingLocation()
to stop every small changes in location...
is there a way to keep track the location only if it oldLocation and newLocation has big different?
Yes there are two ways you can achieve that. First way is to use
func startMonitoringSignificantLocationChanges()
startMonitoringSignificantLocationChanges starts the generation of updates based on significant location changes. Apps can expect a notification as soon as the device moves 500 meters when you are using startMonitoringSignificantLocationChanges. And if you want to use startUpdatingLocation then set distanceFilter to distance of your choice and it will report location only if location change is more than distance specified in filter. distanceFilter represents the minimum distance (measured in meters) a device must move horizontally before an update event is generated.
I'm building an app that uses CoreLocation to get user's coordinates. After I start updating location, I'll get the coordinates in didUpdateLocations. Probably because of gps calibration, in didUpdateLocations I get 2 or 3 different coordinates, until it stops. How can I call a function after updating locations has stopped that uses the last founded coordinates?
Allow didUpdateLocations: to be called repeatedly, as you rightly say, until either you hit some kind of timeout or your get sufficient horizontal accuracy. Then, right from didUpdateLocations:, tell the location manager to stop, and call the function.