Get traveled distance using GPS
I would like to track my traveled distance with my car. It would start recording when my speed would be greater than 0 in KM/H
I got my speed with this code : speed = location.getSpeed() * 3.6;
Wo开发者_如何学编程uld like to do this:
if (speed > 0) //track my distance.
I only found how to calculate distance betwen two points. Thank you for helping.
I think testing speed (or orientation) overcomplicates this.
If optimising battery life isn't a concern (e.g. you're using a car charger), I'd go with sampling the position every n seconds, and updating the distance travelled based on current and last co-ordinates.
If the car hasn't moved, then the GPS co-ordinates shouldn't have changed (except maybe due to accuracy issues), so the distance travelled wouldn't change when stationary.
If you're only interested in distance, not the route, then you only need 2 points, current and previous.
Hope this helps,
Phil Lello
- Keep a list of all the points you have recorded
- for each point in the list, compute the distance to the next one (apparently you know how to do this) and add it to the totalDistance.
This is how I would approach that interesting issue:
- Use Sensor.TYPE_ORIENTATION to conclude when your car changes direction , e.g. turning. Define some treshold to accurately conclude that and at that point get GPS position
- If you are driving long time on a highway (no corners), add periodic GPS point sampling
- Calculate distance between two last points in the list and get distance between two points
- Add values to get cumulative distance in real time
This way you approximate your path with linear segments which is close to the real value.
精彩评论