Android:GPS - best way to count kilometers - android

I have one app which count distance in kilometers with GPS.
And my problem is that my app delayed the kilometers.
I use this to update the coordinates.
lm.requestLocationUpdates(lm.GPS_PROVIDER, 180,1, Loclist);
I calculate for 1 meter with 200km\h is take 180 ms.
But maybe this is wrong,because i again get slow distance kilometers.
I don't know what is the best way for updates to travel kilometers with car.
I use this code to calculate the distance.
Location locationA = new Location("point A");
locationA.setLatitude(lastLat);
locationA.setLongitude(lastLon);
Location locationB = new Location("point B");
locationB.setLatitude(currentLat);
locationB.setLongitude(currentLon);
distanceMeters = locationA.distanceTo(locationB);
Sorry for my bad english.
Thanks in advance.

GPS chips (and the one in your phone) delivers locations not more than once a second.
So summing up the distances, maybe with a bit of filtering, gives you the correct distance.
of course the distance can be 1-1,5 s delayed, it depends on your app if you want to estiamte the distance drievn since last location update, or if you want to measure only the measured locations.
if you extrapolate then it is clear that later it can turn out that the estimation was wrong( e.g when decelerating the car).
So my advise, you should only sum up what you have alady got from Gps.

speed = lm.getSpeed();
if(first_kilometer)
timeTaken =TimeNow - TimeStarted;
else
timeTaken = TimeNow - TimeLastKilometer;
then if you get the 30Km/h..then 30/60 minutes = time taken to get a kilometer.
(speed/60) = timeTakenToGetKilometer;
TimeLastKilometer = TimeNow;
kilometer++;

Related

Android - How to get those locations from my database what are within a given distance around my location?

I found a lot of solutions to calculate the distance between two locations, just like
how to find the distance between two geopoints?
or
Calculating distance between two geographic locations
or to be more specific, here's a code for the .distanceTo method:
Location locationA = new Location("point A");
locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
locationA.setLongitude(pointA.getLongitudeE6() / 1E6);
Location locationB = new Location("point B");
locationB.setLatitude(pointB.getLatitudeE6() / 1E6);
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);
double distance = locationA.distanceTo(locationB);
If I have a location (let's say "locationA"), and I have a database with POI-location data, and I'm interested in only those, what are around that location "locationA" within a given distance (let's say "distance"), how do I get those?
If I write a method
private Location[] poiAroundMe (Location locationA, double distance) {
// The necessary calculation
return locationsAroundMeWithinTheGivenDistance[];
}
what would be "the necessary calculation"?
I'm pretty sure, that there's no built-in method for that, so I would like to ask for help.
Thank you!
Do a select on your database, to get all the locations which are inside of a square that includes exactly your "distance-circle".
SELECT *
FROM TblLocations
WHERE longitude < x
AND longitude > y
AND latitude < a
AND latitude > b;
Put the data in a array, and give it to your poiAroundMe() -Method.
Inside you can check each for the distance to your locationA with a calcualte-method you already know and filter all, which have a smaller distance to your locationA and put them into your locationsAroundMeWithinTheGivenDistance[]
It's not as clean as you perhaps wish, but it will work i think. ;)

Location.distanceTo not giving correct results in android

I am trying to calculate the distance between two locations (Current location with the previous location).
So I tried the following:
Location previousLocation = new Location("");
previousLocation.setLatitude(sharedPreferences.getFloat("previousLatitude", 0f));
previousLocation.setLongitude(sharedPreferences.getFloat("previousLongitude", 0f));
float distance = location.distanceTo(previousLocation);
totalDistanceInMeters += distance;
editor.putFloat("totalDistanceInMeters", totalDistanceInMeters);
Log.e("Location Update","totalDistance"+totalDistanceInMeters);
if (totalDistanceInMeters > 1)
{
Toast.makeText(getApplicationContext(), "Total UpdateLocation"+totalDistanceInMeters/1609, Toast.LENGTH_SHORT).show();
Log.e("Alert","Update");
}
To test the above code. The first time result was perfect and when it triggered the second time. The phone was in the same location but I am getting distances like 141.0111 m. thrid time 304.0011 m. Am I doing something wrong here?
The results are not showing up correctly. According to doc online the results are in metres.
Is there an easy way to calculate the difference between the first location results with the second one and if it is more than 10m I would like to do some other calculation if not just keep quite.
Let me know.
why are you even using the following code
float distance = location.distanceTo(previousLocation);
totalDistanceInMeters += distance;
That adds up the previous distance to present distance and gives the added value everytime....
example
first time
distance between A and B is 100 m
second time
distance between A and B is 100 m+100 m=200 m
so try using distance directly in toast
float distance = location.distanceTo(previousLocation);
if (totalDistanceInMeters > 1)
{
Toast.makeText(getApplicationContext(), "Total UpdateLocation"+distance/1609,Toast.LENGTH_SHORT).show();
Log.e("Alert","Update");
}
I think accuracy needs to be set in order to get the exact distance. Also try to get hold of the previous and current locations manually so as to calculate the distance and verify.

Android GPS accuracy in calculating distance issues

The problem I believe is well known.
So what is my problem?
I am writing an app that is using the GPS provider to find the location of the user and calculate the distance from the user to the end point. This works correctly. However a problem occurs:
The problem in details:
If you go around the city with my app it will give you some numbers for your distance but ... the distance is aways changing. No matter if I move towards my destination the distance either goes higher or goes lower (the value of the distance calculated is "jumping" around from high to low and vise versa) the the previous number of the distance but logically it should be getting lower. I believe this is because the GPS signal is sometimes lost or weak and it cant calculate the distance correctly.
What I need help with?
I want to know is there a way to filter the coordinates received from the GPS so I can get more accurate numbers for distance so when I move towards my end point the distance is calculated correctly(as possible not necessary to be 100% correct) and not go up and down the scales like crazy.
How do I get the coordinates and calculate the distance:
public void onLocationChanged(Location location)
{
txtLat = (TextView) findViewById(R.id.currentCoordinatesView);
clat = location.getLatitude();
clong = location.getLongitude();
Location location1 = new Location("Start");
location1.setLatitude(clat);
location1.setLongitude(clong);
Location locationB = new Location("Finish");
locationB.setLatitude(endLatitude); //endpoint coordinates
locationB.setLongitude(endLongitude);
distance = location1.distanceTo(locationB); //calculate the distance
TextView TextDistance = (TextView)findViewById(R.id.TextDistance);
TextDistance.setText(new DecimalFormat("##,###,###.##").format(distance)+" m");
CurLat = location.getLatitude();
CurLong = location.getLongitude();
The below answer can be further improved by saving the last, say, 10 location objects and do calculations based on those. If for instance the 10 last locations suggests that the user is moving towards the target at 1m/s, then a new location suggesting a jump of 5 meters is very likely inaccurate and should be ignored.
To filter some of the GPS updated which are way off you could simple do something like this (3 represent how accurate the position should be in respect to your actual position and may be adjusted):
public void onLocationChanged(Location location)
{
if (location.hasAccuracy() && location.getAccuracy() < 3) {
txtLat = (TextView) findViewById(R.id.currentCoordinatesView);
clat = location.getLatitude();
clong = location.getLongitude();
Location location1 = new Location("Start");
location1.setLatitude(clat);
location1.setLongitude(clong);
Location locationB = new Location("Finish");
locationB.setLatitude(endLatitude); //endpoint coordinates
locationB.setLongitude(endLongitude);
distance = location1.distanceTo(locationB); //calculate the distance
TextView TextDistance = (TextView)findViewById(R.id.TextDistance);
TextDistance.setText(new DecimalFormat("##,###,###.##").format(distance)+" m");
CurLat = location.getLatitude();
CurLong = location.getLongitude();
}
}
Here is the definition of the accuracy measure from: Location getAccuracy()
Get the estimated accuracy of this location, in meters.
We define accuracy as the radius of 68% confidence. In other words, if you draw a circle centered at this location's latitude and longitude, and with a radius equal to the accuracy, then there is a 68% probability that the true location is inside the circle.
In statistical terms, it is assumed that location errors are random with a normal distribution, so the 68% confidence circle represents one standard deviation. Note that in practice, location errors do not always follow such a simple distribution.
This accuracy estimation is only concerned with horizontal accuracy, and does not indicate the accuracy of bearing, velocity or altitude if those are included in this Location.
If this location does not have an accuracy, then 0.0 is returned. All locations generated by the LocationManager include an accuracy.

Check if device is really moving

I am working on an application where I need to get the speed of a car. To get the speed, I know I can use something like double speed =locationB.getSpeed();` however when I am testing, the speed varies between 0.0 and 40 km/h when I am just sitting right behind my laptop not moving at all. In the car, the speed actually comes close to the cars speed, so that shouldn't be a problem.
What would be the best way to check if the device is really moving? I've already tried to get the distance between locationA and locationB and use that with the time it took to get the 2 locations, to get the speed.
double distance = locationA.distanceTo(locationB);
double speed = (distance / time) * 3600 / 1000;
However this seems to be not stable at all, like the getSpeed() method.
Is there a way to only display the speed if the device is moving? And would it be reliable?
Any help is appreciated,
Thanks.
Check the horicontal accuracy attribute of Location.
If it is under 30m you can ignore the location.
If you are sitting on your laptop and get speed = 40km/h (which I never saw in good GPS devices), then look what the hor. accuracy is.
It probably is much over 30m.
In GPS based systems, never ever calculate the speed by positional change in time,
just use the location.getSpeed().
The reason is that the GPS chip internally calculates the speed via physical doppler effect, not via positional change.
While standing still, or at very low speeds this does not work well, so you have to filter out very low speeds, and bad gps signal. (via horicontal accuracy estimate)
I think you should limit the distance between A and B to be a minimum length. Small distances will introduce more error into your speed calculations.
Boolean moving - false;
double distance = locationA.distanceTo(locationB);
double speed = (distance / time) * 3600 / 1000;
if (distance > SOME_THRESHOLD) {
moving = true
}

How to get straight distance between two location in android?

First read Question carefully ...
I need straight distance, not by walking,car,or etc.
Take a look to this image which given below,
Google provide us distance by car and driving.
But I don't want it, I want straight distance between two location (latitude - longitude).
Which is displayed as as RED LINE.
NOTE : I don't want to put red line on Google map, just want the Distance in Units(mile,km,etc.)
ANDROID
double distance
Location locationA = new Location(“point A”)
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location(“point B”);
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
MATHEMATICALY
a = distance in degrees //meterConversion = 1609;
b = 90 - latitude of point 1
c = 90 - latitude of point 2
l = longitude of point 1 - longitude of point 2
Cos(a) = Cos(b)Cos(c) + Sin(b)Sin(c)Sin(l)
d = circumference of Earth * a / 360 // circumference of Earth = 3958.7558657440545D km
The Haversine function is used to find the distance between two points on a sphere.
It's fairly straightforward to extend this to finding the straight line distance between two points on the Earth. The Earth is not a perfect sphere, but this is still a good approximation using a standard measurement (called WGS84) for the radius at the equator.
As CommonsWare has said, you can do this very simply by using distanceBetween(), which uses the Haversine function and the WGS84 radius.
For better understanding of implementation/math, take a look at this sample code in Python.
Distance you find with following code.
You just need to get two geoPoint's latitude and longitude.
and use that in following calculation to get distance.
R = 6371; // km
d = Math.acos(Math.sin(lat1)*Math.sin(lat2) +
Math.cos(lat1)*Math.cos(lat2) *
Math.cos(lon2-lon1)) * R;
That will be return distance after all calculation.
R is the radius of surface in KM, need to use in calculation and you try this. I hope it is useful for you.

Categories

Resources