So my rounding code should look something like:
distancemi = math.round(distancemi)\*0.1
Sorry, I’m bad at math.
So my rounding code should look something like:
distancemi = math.round(distancemi)\*0.1
Sorry, I’m bad at math.
Hi. I wanted to interject that you’re working too hard. Don’t write the code to calculate delta distance yourself. Let some else do it.
There is an equation called the “Haversine Distance”. I’ve borrowed someone else’s code and you can now borrow it from me:
-- Calculate the distance from one decimal lat-long position to another. -- Distance is a multiple of R (either kilometers or miles) -- More accurate for short distances than long. function math.haversine\_dist( lat1, lng1, lat2, lng2, R ) --[[haversine formula dlon = lon2 - lon1 dlat = lat2 - lat1 a = (sin(dlat/2))^2 + cos(lat1) \* cos(lat2) \* (sin(dlon/2))^2 c = 2 \* atan2( sqrt(a), sqrt(1-a) ) d = R \* c (where R is the radius of the Earth; radius of the Earth: 3961 miles and 6373 km) --]] local mRad = math.rad local mCos = math.cos local mSin = math.sin local mSqrt = math.sqrt local mAtan2 = math.atan2 R = R or 6373 -- Default radius of earth in km local dlng = mRad(lng2 - lng1) local dlat = mRad(lat2 - lat1) lat1 = mRad(lat1) lat2 = mRad(lat2) local a = (mSin(dlat/2))^2 + mCos(lat1) \* mCos(lat2) \* (mSin(dlng/2))^2 local c = 2 \* mAtan2( mSqrt(a), mSqrt(1-a) ) local d = R \* c --(where R is the radius of the Earth) return d end
Now, if you have start lat-lng and current lat-lng you can get the delta distance like this:
print( "Delta Dist in kilometers: ", math.haversine\_dist( startLat, startLng, curLat, curLng, 6373 ) ) print( "Delta Dist in miles: ", math.haversine\_dist( startLat, startLng, curLat, curLng, 3961 ) )
Thanks roaminggamer! output seems to be more accurate but again how do I round it up to one digit after dot correctly?
I don’t think you should. 0.1 is 568 feet. It sounds like you need more resolution to this. 0.01 is 56.8 feet and 0.001 is 5.68 feet. You can’t measure small movements without enough resolution.
Now what I would do is not round at all while you’re doing your math, but output the visual like this:
distanceText.text = string.format("%0.1f", distancemi)
Rob
-- == -- round(val, n) - Rounds a number to the nearest decimal places. (http://lua-users.org/wiki/FormattingNumbers) -- val - The value to round. -- n - Number of decimal places to round to. -- == function math.round2(val, n) if (n) then return math.floor( (val \* 10^n) + 0.5) / (10^n) else return math.floor(val+0.5) end end
Or use format as suggested by rob to get a string formatted number.
Thanks guys for your help!
Any tips on how to reduce or eliminate this “garbage” location collecting (even if still, the phone receives different location coordinates)?
Not sure I follow. Can you explain better what is happening and what you want to do?
Thanks
Rob
Hi, I am working on an app with gps as well. I have seen in my log data the first gps readings may come from cache on the device. So always ignore the two first readings, that already minimizes “garbage”. I am not there yet with getting no garbage at all, but this helps.
Here’s the way location services works. It first gets a location from WiFi if WiFi is enabled or from 3G/4G if it has support for that. 3G/4G is the least accurate, WiFi in the middle and GPS The most.
You have probably seen this behavior in the Maps app. It starts showing a big circle around you, then it gets smaller and smaller. With 3G/4G services as it contacts more towers the more accurate it gets up to a limit. If you are on WiFi, it’s getting the location of the nearest WiFi hot spot or your home WiFi.
GPS can take a while to connect. If you’re in a building, it may not even be able to see the GPS birds. It as to be able to get the signal from at least three satellites to triangulate your position. These don’t sync up immediately.
These first few locations that you are throwing a way is just the device homing in on you. If you can’t see the GPS satellites then those first few signals may be as accurate as you can get.
Next, the GPS has errors built in. Even with the Map app, if you zoom in, don’t move, you will see your dot move around by a few meters. Without moving, I can be in my living room, then a few seconds later, I’m in my garage at my house. It’s the nature of GPS.
Rob
Rob,
Thanks for the answer. I already saw that when driving the car the overall accuracy of speed and also calculated distance is a lot better then when walking. I am still “tuning” to get more accurate readings when running, since I am working on a running app. The reported accuracy can be used I guess to determine how many decimals of the latitude and longitude can be considered valuable. When the accuracy is above 100 meters, you could choose to only use the first three decimals for calculating distance? And so on based on this table (http://gis.stackexchange.com/questions/8650/how-to-measure-the-accuracy-of-latitude-and-longitude).
Also based on this table I am ignoring everything from the 4th/5th decimal since that does explain the “moving around” when you are not moving :-).
decimal places degrees distance ------- ------- -------- 0 1 111 km 1 0.1 11.1 km 2 0.01 1.11 km 3 0.001 111 m 4 0.0001 11.1 m 5 0.00001 1.11 m 6 0.000001 11.1 cm 7 0.0000001 1.11 cm 8 0.00000001 1.11 mm
Hi, I am working on an app with gps as well. I have seen in my log data the first gps readings may come from cache on the device. So always ignore the two first readings, that already minimizes “garbage”. I am not there yet with getting no garbage at all, but this helps.
Here’s the way location services works. It first gets a location from WiFi if WiFi is enabled or from 3G/4G if it has support for that. 3G/4G is the least accurate, WiFi in the middle and GPS The most.
You have probably seen this behavior in the Maps app. It starts showing a big circle around you, then it gets smaller and smaller. With 3G/4G services as it contacts more towers the more accurate it gets up to a limit. If you are on WiFi, it’s getting the location of the nearest WiFi hot spot or your home WiFi.
GPS can take a while to connect. If you’re in a building, it may not even be able to see the GPS birds. It as to be able to get the signal from at least three satellites to triangulate your position. These don’t sync up immediately.
These first few locations that you are throwing a way is just the device homing in on you. If you can’t see the GPS satellites then those first few signals may be as accurate as you can get.
Next, the GPS has errors built in. Even with the Map app, if you zoom in, don’t move, you will see your dot move around by a few meters. Without moving, I can be in my living room, then a few seconds later, I’m in my garage at my house. It’s the nature of GPS.
Rob
Rob,
Thanks for the answer. I already saw that when driving the car the overall accuracy of speed and also calculated distance is a lot better then when walking. I am still “tuning” to get more accurate readings when running, since I am working on a running app. The reported accuracy can be used I guess to determine how many decimals of the latitude and longitude can be considered valuable. When the accuracy is above 100 meters, you could choose to only use the first three decimals for calculating distance? And so on based on this table (http://gis.stackexchange.com/questions/8650/how-to-measure-the-accuracy-of-latitude-and-longitude).
Also based on this table I am ignoring everything from the 4th/5th decimal since that does explain the “moving around” when you are not moving :-).
decimal places degrees distance ------- ------- -------- 0 1 111 km 1 0.1 11.1 km 2 0.01 1.11 km 3 0.001 111 m 4 0.0001 11.1 m 5 0.00001 1.11 m 6 0.000001 11.1 cm 7 0.0000001 1.11 cm 8 0.00000001 1.11 mm