Accuracy of Core Location - ios

I'm currently working on a location tracking app and I have difficulties with inaccurate location updates from my CLLocationManager. This causes my app to track distance which is in fact only caused by inaccurate GPS readings.
I can even leave my iPhone on the table with my app turned on and in few minutes my app tracks hundreds of meters worth of distance just because of this flaw.
Here's my initialization code:
- (void)initializeTracking {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 5;
[self.locationManager startUpdatingLocation];
}
Thanks in advance! :-)

One of the ways I solved this in a similar application is to discard location updates where the distance change is somewhat less than the horizontal accuracy reported in that location update.
Given a previousLocation, then for a newLocation, compute distance from the previousLocation. If that distance >= (horizontalAccuracy * 0.5) then we used that location and that location becomes our new previousLocation. If the distance is less then we discard that location update, don't change previousLocation and wait for the next location update.
That worked well for our purposes, you might try something like that. If you still find too many updates that are noise, increase the 0.5 factor, maybe try 0.66.
You may also want to guard against cases when you are just starting to get a fix, where you get a series of location updates that appear to move but really what is happening is that the accuracy is improving significantly.
I would avoid starting any location tracking or distance measuring with a horizontal accuracy > 70 meters. Those are poor quality positions for GNSS, although that may be all you get when in an urban canyon, under heavy tree canopy, or other poor signal conditions.

I've used this method to retrive the desired accuracy of the location (In SWIFT)
let TIMEOUT_INTERVAL = 3.0
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let newLocation = locations.last as! CLLocation
println("didupdateLastLocation \(newLocation)")
//The last location must not be capured more then 3 seconds ago
if newLocation.timestamp.timeIntervalSinceNow > -3 &&
newLocation.horizontalAccuracy > 0 {
var distance = CLLocationDistance(DBL_MAX)
if let location = self.lastLocation {
distance = newLocation.distanceFromLocation(location)
}
if self.lastLocation == nil ||
self.lastLocation!.horizontalAccuracy > newLocation.horizontalAccuracy {
self.lastLocation = newLocation
if newLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy {
//Desired location Found
println("LOCATION FOUND")
self.stopLocationManager()
}
} else if distance < 1 {
let timerInterval = newLocation.timestamp.timeIntervalSinceDate(self.lastLocation!.timestamp)
if timerInterval >= TIMEOUT_INTERVAL {
//Force Stop
stopLocationManager()
}
}
}
Where:
if newLocation.timestamp.timeIntervalSinceNow > -3 &&
newLocation.horizontalAccuracy > 0 {
The last location retrieved must not be captured more then 3 seconds ago and the last location must have a valid horizontal accuracy (if less then 1 means that it's not a valid location).
Then we're going to set a distance with a default value:
var distance = CLLocationDistance(DBL_MAX)
Calculate the distance from the last location retrieved to the new location:
if let location = self.lastLocation {
distance = newLocation.distanceFromLocation(location)
}
If our local last location hasn't been setted yet or if the new location horizontally accuracy it's better then the actual one, then we are going to set our local location to the new location:
if self.lastLocation == nil ||
self.lastLocation!.horizontalAccuracy > newLocation.horizontalAccuracy {
self.lastLocation = newLocation
The next step it's to check whether the accuracy from the location retrieved it's good enough. To do that we check if the horizontalDistance of the location retrieved it lest then the desiredAccurancy. If this is case we can stop our manager:
if newLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy {
//Desired location Found
self.stopLocationManager()
}
With the last if we're going to check if the distance from the last location retrieved and the new location it's less the one (that means that the 2 locations are very close). If this it's the case then we're going to get the time interval from the last location retrieved and the new location retrieved, and check if the interval it's more then 3 seconds. If this is the case, this mean that it's more then 3 seconds that we're not receiving a location which is more accurate of the our local location, and so we can stop the location services:
else if distance < 1 {
let timerInterval = newLocation.timestamp.timeIntervalSinceDate(self.lastLocation!.timestamp)
if timerInterval >= TIMEOUT_INTERVAL {
//Force Stop
println("Stop location timeout")
stopLocationManager()
}
}

This is always a problem with satellite locations. It is an estimate and estimates can vary. Each new report is a new estimate. What you need is a position clamp that ignores values when there is no movement.
You might try to use sensors to know if the device is actually moving. Look at accelerometer data, if it isn't changing then the device isn't moving even though GPS says it is. Of course, there is noise on the accelerometer data so you have to filter that out also.
This is a tricky problem to solve.

There's really not a whole lot more you can do to improve what the operating system and your current reception gives to you. On first look it doesn't look like there's anything wrong with your code - when it comes to iOS location updates you're really at the mercy of the OS and your service.
What you CAN do is control what locations you pay attention to. If I were you in my didUpdateLocations function when you get callbacks from the OS with new locations - you could ignore any locations with horizontal accuracies greater than some predefined threshold, maybe 25m? You would end up with less location updates to use but you'd have less noise.

Related

CLLocationManager - returns wrong speed

I am trying to calculate users current driving speed, but there is a huge difference between cllocationmanager speed and actual driving speed.
As I am driving at 50 kmph and cllocation manager shows ~72/~73 kmph. Below is the code I am using.
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.requestWhenInUseAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.distanceFilter = 1.0
locationManager.headingFilter = 0.1
locationManager.startUpdatingLocation()
And below is the location manager protocol
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let speedInKmph = location.speed * 3.6
if speedInKmph > 10 {
MyRide.shared.speedInfo.append(SpeedInfo(speed: speedInKmph))
self.view.showToast("\(speedInKmph) **********", position: .bottom, popTime: 2.0, dismissOnTap: false)
}
}
#Anand, the CLLocationManager could be obtaining speed on one of two ways:
measuring the distance between two lat/long/alt triplets and dividing by some sort of time reference
using the native Doppler speed measurement on the GPS chip. Doppler speed is computed for all of the satellites that the particular GPS chip sees. Maybe Apple publishes the names and model numbers of the GPS chips inside of iPhones, I don't know.
I don't know which technique is used by CLLocationManager.
I think you have to test your system at a couple of speeds and three directions (due north, due east, and 45 degrees angle to due North and due East).
If all measurements show the same fudge factor of 1.45, you are probably good to go with:
let speedInKmph = location.speed * 3.6 * (1/1.45)
If your measurements don't match each other, then more studying required. If I figure it out I will post it here. The linked youtube might be a good idea.

Measure distance between two location inaccurate

am trying to get the distance between two location, but I always get wrong value. Here are the codes:
var startLoction: CLLocation!
var startLoction: CLLocation!
#IBAction func getCurrLocation(){
startLoction = currLocation
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currLocation = locations.last!
if currLocation.horizontalAccuracy > 0{
var distance = CLLocationDistance(DBL_MAX)
if let location = self.startLoction {
distance = currLocation.distance(from: location)
realTimeDistanceLabel.text = String(format: "%.2f", distance)
}
}
}
Problems:
1. When I stay still, the distance sometimes will increase, it may be a big number than 15 meters. It always starts at a big number
2. When I walk out 10 or 20 meters and then walk back straight, the distance sometimes increases but not decreases.
3. When I walk around a big circle, the distance goes to a bit more accurate value relatively.
I also tried to addcurrLocation.horizontalAccuracy > 0 && currLocation.horizontalAccuracy < 100, and aslo tried prevLocation >= (currLocation.horizontalAccuracy * 0.5 from stackOverflow answer, still I cannot get a accurate value.
Any other ideas to make it right?
Thanks.
Core Location switch on when used, spend a period triangulating the device location such that it takes a while to settle down. But also you will find it can lose accuracy in certain locations/conditions and will try always to improve accuracy. The device will then be changing it's mind about "where you are located" and if it does this after your method
#IBAction func getCurrLocation() { ... }
is called, naturally it will appear as though the device has moved.
To reduce the effect of this occurring, you could test not just that currLocation.horizontalAccuracy > 0 (so it is a valid reading) but also that it is less than a given positive value (though this will bring it's own problem - see below). The current horizontal location of the device may be between plus or minus the radius of uncertainty as reported by the horizontalAccuracy property (the radius of uncertainty is always a positive number. If it is negative, all bets are off, the device is essentially saying "hang on, I can't say where I am with any certainty yet").
Actually though setting an upper bound for currLocation.horizontalAccuracy will demonstrate the problem, it probably won't be what you want because then your UI will only work under the condition Core Location knows accuracy is greater than x. Generally you wouldn't want to restrict the UI in this way.

How to display current speed in MPH based on data from GPS and Accelerometer. I have an idea, but having difficulty executing it

I apologize for not knowing the proper terminology for everything here. I'm a fairly new programmer, and entirely new to Swift. The task I'm trying to accomplish is to display a current speed in MPH. I've found that using the "CoreLocation" and storing locations in an array and using "locations.speed "to display the speed is quite slow and does not refresh as often as I want.
My thought was to get an initial speed value using the "MapKit" and "CoreLocation" method, then feed that initial speed value into a function using the accelerometer to provide a quicker responding speedometer. I would do this by integrating the accelerometer values and adding the initial velocity. This was the best solution I could come up with to get a more accurate speedometer with a better refresh rate.
I'm having a couple of issues currently:
First Issue: I don't know how to get an initial speed value from a function using location data as parameters into a function using accelerometer data as parameters.
Second Issue: Even when assuming an initial speed of 0, my current program displays a value that keeps increasing infinitely. I'm not sure what the issue is that is causing this.
I will show you the portion of my code responsible for this, and would appreciate any insight any of you may have!
For my First Issue, here is my GPS data Function:
func provideInitSpeed(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])->Double {
let location = locations[0]
return ((location.speed)*2.23693629) //returns speed value converted to MPH
}
I'm not sure how to make a function call to retrieve this value in my Accelerometer function.
For my Second Issue, here is my Accelerometer Function with assumed starting speed of 0:
motionManager.startAccelerometerUpdates(to: OperationQueue.current!) {
(data,error) in
if let myData = data {
//getting my acceleration data and rounding the values off to the hundredths place to reduce noise
xAccel = round((myData.acceleration.x * g)*100)/100
yAccel = round((myData.acceleration.y * g)*100)/100
zAccel = round((myData.acceleration.z * g)*100)/100
// Integrating accel vals to get velocity vals *Possibly where error occurs* I multiply the accel values by the change in time, which is currently set at 0.2 seconds.
xVel += xAccel * self.motionManager.accelerometerUpdateInterval
yVel += yAccel * self.motionManager.accelerometerUpdateInterval
zVel += zAccel * self.motionManager.accelerometerUpdateInterval
// Finding total speed; Magnitude of Velocity
totalSpeed = sqrt(pow(xVel,2) + pow(yVel,2) + pow(zVel,2))
// if-else statment for further noise reduction. note: "zComp" just adjusts for the -1.0 G units z acceleration value that the phone reads by default for gravity
if (totalSpeed - zComp) > -0.1 && (totalSpeed - zComp) < 0.1 {
self.view.reloadInputViews()
self.speedWithAccelLabel.text = "\(0.0)"
} else {
// Printing totalSpeed
self.view.reloadInputViews()
self.speedWithAccelLabel.text = "\(abs(round((totalSpeed - zComp + /*where initSpeed would go*/)*10)/10))"
}
}//data end
}//motionManager end
I'm not sure why but the speed this function displays is always increasing by about 4 mph every refresh of the label.
This is my first time using Stack Overflow, so I apologize for any stupid mistakes I might have made!
Thanks a lot!

Using iOS device as teslameter (magnetometer discussion)

In my app I want to detect how strong surrounding magnetic/electromagnetic fields are. What I want to achieve is to measure magnetic field change to know if it's stronger than in control measurement or if it's lower. This is my code:
- (void)setupLocationManager {
self.locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager headingAvailable] == NO) {
self.locationManager = nil;
} else {
self.locationManager.headingFilter = kCLHeadingFilterNone;
self.locationManager.delegate = self;
[self.locationManager startUpdatingHeading];
}
}
// CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading {
CGFloat magnitude = sqrt(heading.x * heading.x + heading.y * heading.y + heading.z * heading.z);
if (self.defaultMagnitudeValue == 0.f) {
self.defaultMagnitudeValue = magnitude;
}
self.curentMagnitudeValue = magnitude;
}
Magnitude value reacts to the magnetic fields surrounding the device, the problem is that you need to be REALLY close to a source of such a field.
So, the question is: Is there any possibility for an iOS app to measure magnetic fields on distances more than 10-20 centimeters? If so, how?
P.S.: I have also checked the Teslameter App by Apple, it has nearly same code and totally same problems.
Sure you can, the magnetic field just has to be very strong!
Your first limit might be the hardware. As noted in In iOS, what is the difference between the Magnetic Field values from the Core Location and Core Motion frameworks? the range of those raw values may be limited. At some some hardware and iOS versions restricted them to -128 to +128 microteslas. On an iPhone 6 I can get microtelsa readings well outside that range but that doesn't necessarily help. Apple doesn't seem to provide any reference for the accuracy of magnetometer readings, we can measure values out to nanotesla but the results we get back might be meaningless noise.
Earth's magnetic field at the surface will be 25 to 65 microteslas. Any field you hope to measure is going to need to be measurably stronger than that at the desired measurement distance. What are you trying to measure, and is it really strong enough to move a compass needle at the distance you want to measure it?

iPhone GPS User Location is moving back and forth while the phone is still

I am doing some mapkit and corelocation programming where I map out a users route. E.g. they go for a walk and it shows the path they took.
On the simulator things are working 100% fine.
On the iPhone I've run into a major snag and I don't know what to do. To determine if the user has 'stopped' I basically check if the speed is (almost) 0 for a certain period of time.
However just keeping the phone still spits out this log for newly updated location changes (from the location manager delegate). These are successive updates in the locationManager(_:didUpdateLocations:) callback.
speed 0.021408926025254 with distance 0.192791659974976
speed 0.0532131983839802 with distance 0.497739230237728
speed 11.9876451887096 with distance 15.4555990691609
speed 0.230133198005176 with distance 3.45235789063791
speed 0.0 with distance 0.0
speed 0.984378335092039 with distance 11.245049843458
speed 0.180509147029171 with distance 2.0615615724029
speed 0.429749086272364 with distance 4.91092459284206
Now I have the accuracy set to best:
_locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.distanceFilter = kCLDistanceFilterNone
_locationManager.desiredAccuracy = kCLLocationAccuracyBest
Do you know if there is a setting or I can change to prevent this back and forth behaviour. Even the user pin moves wildly left and right every few seconds when the phone is still.
Or is there something else I need to code to account for this wild swaggering?
I check if the user has moved a certain distance within a certain time to determine if they have stopped (thanks to rmaddy for the info):
/**
Return true if user is stopped. Because GPS is in accurate user must pass a threshold distance to be considered stopped.
*/
private func userHasStopped() -> Bool
{
// No stop checks yet so false and set new location
if (_lastLocationForStopAnalysis == nil)
{
_lastLocationForStopAnalysis = _currentLocation
return false
}
// If the distance is greater than the 'not stopped' threshold, set a new location
if (_lastLocationForStopAnalysis.distanceFromLocation(_currentLocation) > 50)
{
_lastLocationForStopAnalysis = _currentLocation
return false
}
// The user has been 'still' for long enough they are considered stopped
if (_currentLocation.timestamp.timeIntervalSinceDate(_lastLocationForStopAnalysis.timestamp) > 180)
{
return true
}
// There hasn't been a timeout or a threshold pass to they haven't stopped yet
return false
}

Resources