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.
Related
why my location is little bit not accurate and some times far from my real location, my real location in red sign and some times my location detect in green sign it's so far from my location
how do i can fix that?
this my code for detect my location
#Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
this.googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
GoogleMapOptions option = new GoogleMapOptions();
option.compassEnabled(true);
this.googleMap.setTrafficEnabled(true);
googleMap.setMyLocationEnabled(true);
googleMap.setOnMyLocationChangeListener(myLocationChangeListener);
}
private OnMyLocationChangeListener myLocationChangeListener = new OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
if(awal) {
awal=false;
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
}
};
Sometimes
In android accurate location is 68% confidence.
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
more details Refer Location
I'm a beginner in android and my question is regarding about google map api v2.I drew a path using my current position and my destination and I got some locations of a bus which is travelling through my path. How can I check the buse's location LatLng values are matching with my path LatLng values???
Try this,
Location crntLocation=new Location("crntlocation");
crntLocation.setLatitude(currentLatitude);
crntLocation.setLongitude(currentLongitude);
Location newLocation=new Location("newlocation");
newLocation.setLatitude(server_latitude);
newLocation.setLongitude(server_longitude);
float distance = crntLocation.distanceTo(newLocation); in meters
//distance =crntLocation.distanceTo(newLocation) / 1000; // in km
if(distance<200){
//You are within 200 meter range
}
Put this in a loop.
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++;
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.
I am writing an android app where a user will geolocate using coarse location using Network Location. I want the user to be able to save a location at a particular time into a db (savedLocation), and then if the user returns to savedLocation, (or within a coarse range of that location since i'm suing network location) I want to fire to a broadcast receiver. I know fine and well how to save the user's location to the db, and to compare it with the current location, etc etc.
However, given that the network location is a relatively inaccurate method for determining location, what would be a good approach for determining when the user approaches within distance X of that savedLocation.
What understand from your question is that you are tracking the user's location and saving that locations to the database. Now you want to prompt user when user reach to the previous location but there should be a specific region. If this is then you can use the below code to know the region of the user from latitude and longitude.
String latitude1, latitude2, longitude1, longitude2;
//Now Suppose you have some values in this variables.
//In your case latitude1 and longitude1 is from database and latitude2 and longitude2 is from current Location.
float radius = 500; //Distance in meters
Location location1 = new Location("GPS");
location1.setLatitude(latitude1);
location1.setLongitude(longitude1);
Location location2 = new Location("GPS");
location2.setLatitude(latitude2);
location2.setLongitude(longitude2);
Float distance = location1.distanceTo(location2);
distance = Math.abs(distance);
//Here you can compare two distances 1. distance and 2. radius
if(distance <= radius) {
//Use is in a 500 meters radius. You can notify user by notification
}