distanceto functon Returning wrong format Android - android

I am calculating distance between two location using latitude and longitude... i am getting the distance easily and converting it into miles... but first difference value come in wrong format and other value coming right.
First distance is coming like this = 2.0E-4
While other like this = 48.8881
i have used the following code
Location l1 = new Location("One");
l1.setLatitude(cur_latitude);
l1.setLongitude(cur_longitude);
for (int i = 0; i < lctn.size(); i++) {
Location l2 = new Location("LocationB");
l2.setLatitude(lctn.get(i).getLocation_lat());
l2.setLongitude(lctn.get(i).getLocation_lng());
float distance = l1.distanceTo(l2);
distance =Float.parseFloat(new DecimalFormat("##.####").format( distance / 1000.0f));
Double mile = Double.parseDouble(new DecimalFormat("##.####").format(distance * 0.6214));
Log.e("km_", "" + distance);
Log.e("miles_", "" + mile);
Also please check whether i am using right formula for calculating miles....Thanks in Advance

Distance is in points that why it happening, You need to use accuracy variable and assign some value to this variable,
in my case i use
int accuracy = 20;
Then compare this accuracy with Location accuracy
if(l2.getAccuracy() <= accuracy ) {
Log.e("km_", "" + distance);
Log.e("miles_", "" + mile);
}
I suggest you to use fused location api for accurate distance.

Related

Geo Coordinates (Long,Lat) to Meters (x,y)

I am working coordinates system as shown below:
x and y are in meters. I am interested in only positive x,y. I want to convert (x,y) to (lat,lon) and vise versa.
I thought it is simple and I dont see a problem with my solutions below. But I am not getting very correct results.
My Solution:
As we see in the image below, I considered the Latitude and Longitude as angles of 2 circles:
1. (x,y) to (lat,lon)
I applied the Arc Length formula (here), shown below, on both; x and y
Hence, my functions are:
private static double calculateLat(float x) {
int earthRadius = 6371000;
return REF_LOC.getLatitude() + x*360/(2*PI*earthRadius);
}
private static double calculateLong(float y) {
int earthRadius = 6371000;
return REF_LOC.getLongitude() + y*360/(2*PI*earthRadius);
}
REF_LOC is the reference Geo Location for which (x,y) are (0,0). It can be any point on earth.
2. (lat,lon) to (x,y)
For this I am simply using this:
int calculateX(double longitude){
Location.distanceBetween(REF_LOC.getLatitude(), REF_LOC.getLongitude(),
REF_LOC.getLatitude(), lonDeg, results);
return results[0];
}
int calculateY(double latitude){
Location.distanceBetween(REF_LOC.getLatitude(), REF_LOC.getLongitude(),
latDeg, REF_LOC.getLongitude(), results);
return results[0];
}
But I am getting inconsistent results. First I use solution 1 and convert some value (x,y) to (lat,long). But when I use the same (lat,long) back to (x,y) using solution 2, I get about 2 meters difference in x and 10 meters in y. Can anyone help me identify the problem please?
Alluding to my comment on spherical vs elliptical calculations, another way to look at difference in distance calculations for spherical vs elliptical distance is to use the 2 available utilities:
// For REF_LOC = (70, 20)
// Compute a lat/lng due east of a REF_LOC and compute distance using both
// available methods.
LatLng P = SphericalUtil.computeOffsetOrigin(REF_LOC, 20000, 90.0);
// And then compute a distance
d = SphericalUtil.computeDistanceBetween(REF_LOC, P);
Location.distanceBetween(REF_LOC.latitude, REF_LOC.longitude, P.latitude, P.longitude, results);
// d = 20000.000000000036
// results[0] = 20081.818
// and for a REF_LOC = (0, 20)
// d = 20000.000000000127
// results[0] = 20022.377
What's also interesting is the SphericalUtil.computeOffsetOrigin() produces error
in latitude increasing from equator to pole making it non-symmetrical. Yet the resulting distances are essentially exact.
I'd recommend using SphericalUtil.computeOffsetOrigin to compute the X/Y breaking
it into the latitude and longitude offsets as you are doing.
Finally demonstrating the SphericalUtil solution:
// Start with some arbitrary x/y relative to REF_LOC
double Rx = 125.0;
double Ry = 73.0;
// Compute a lat/lon from REF_LOC to the point
LatLng Rll = new LatLng(SphericalUtil.computeOffsetOrigin(REF_LOC, Ry, 180).latitude,
SphericalUtil.computeOffsetOrigin(REF_LOC, Rx, 270).longitude);
// And recompute the x/y components of the lat/lon
double Rxx = SphericalUtil.computeDistanceBetween(REF_LOC, new LatLng(REF_LOC.latitude, Rll.longitude));
double Ryy = SphericalUtil.computeDistanceBetween(REF_LOC, new LatLng(Rll.latitude, REF_LOC.longitude));
Resulting in:
Rx/Ry = (125.0, 73.0)
Rxx/Ryy = (125.00000004545973, 73.00000000137051)
Acceptable error I assume.
So the parting question is - what does the x/y really represent?
Reference the source for both utilities for more information:
Location
SphericalUtil

Android GPS distance Calculation

I am trying to calculate the distance between multiple GPS destinations.
My Approach
I am using Google's Matrix API for this, but it allows at max. 25 points. And I need to track the complete distance travelled by a user.
Any suggestion will be very helpful.
Using Android SDK:
Try using the Location object's distanceTo function like so:
float getTripDistance(List<LatLng> vertices) {
float totalDistance = 0;
for (int i = 0; i < vertices.size() - 1; i++) {
Location tLoc1 = new Location("");
Location tLoc2 = new Location("");
tLoc1.setLatitude(vertices.get(i).latitude);
tLoc1.setLongitude(vertices.get(i).longitude);
tLoc2.setLatitude(vertices.get(i + 1).latitude);
tLoc2.setLongitude(vertices.get(i + 1).longitude);
totalDistance += tLoc1.distanceTo(tLoc2);
}
return totalDistance;
}
You can loop over all of your points sequentially and sum each respective distance in meters to get the total trip distance.
I know this is too late but this is for users who are still searching for a solution.
float[] distanceResults = new float[1];
Location.distanceBetween(oldPosition.latitude, oldPosition.longitude,
newPosition.latitude, newPosition.longitude, distanceResults);
As per documentation, the above function computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Hence, the variable distanceResults will have the distance in meters.

Start an Activity based on the location

i want to create an activity which will restrict the users to access the activity inside some location parameters. if they are outside then they cannot access the activity. The location parameter is big or large area, so access outside even upto 50Km is not an issue. but how to make it restrict inside the particular confinement?
Edit:
i am asking how to define the location restriction?, i am asking if the location is 100N and 100E, so how will i restrict, or can i restrict it using places name?
can anyone explain with some example or a bit of code!?
if you integrated the location api, you can use this to calculate the distance between your particular location and the current location
Location currentLoc;
float radius = 50.0;
float distance = loc.distanceTo(PARTICULAR_LOCATION);
if (distance < radius) {
//start activity
}
here the PARTICULAR_LOCATION is the location of the some area you mentiond
You can calculate the Distance between the Location of the Person and the Location with parameters as
Location personLocation =...; // Person's Location
Location restrictedLocation =...; // Location with constraints
// Calculate distance in meters to person's location from restricted location
float distance = restrictedLocation.distanceTo(personLocation);
float distanceInKms = distance / 1000;
if (distanceInKms > 50) {
// Place code for person outside restricted location's 50 KM radius
} else {
// Place code for person inside restricted location's 50 KM radius
}
In your onLocationChanged you can check if a user is inside a given location or not
#Override
public void onLocationChanged(Location location) {
float[] distance = new float[2];
Location.distanceBetween( location.getLatitude(), location.getLongitude(),
locationtomonitor.latitude, locationtomonitor.longitude, distance);
/// distance in meters
if( distance[0] > 1 ){
Toast.makeText(getBaseContext(), " Outside, Distance in meters: " + locationtomonitor.getRadius(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Inside, distance in meters : " + distance[0] , Toast.LENGTH_LONG).show();
}
}
if you want to check a specific country or city, you can use IP to location:
just send a simple get Request to http://ip-api.com/json url and you will get a json data containing info about country, city, zip code etc.

Why does Android location.distanceTo return values in Millions?

In my Android application I am trying to calculate the distance between two locations but the values I am getting is in tens of Millions 11Million+. The actual distance between the two point/location is just 1.1km - 1.3Km. Why is this so? Even if the value the .distanceTo method returns is in meters 11M meters is still a very big value.
Here is my code:
Location locationA = new Location("LocationA");
locationA.setLatitude(lat);
locationA.setLongitude(lang);
Location locationB = new Location("LocationB");
locationB.setLatitude(14.575224);
locationB.setLongitude(121.042475);
float distance = locationA.distanceTo(locationB);
BigDecimal _bdDistance;
_bdDistance = round(distance,2);
String _strDistance = _bdDistance.toString();
Toast.makeText(this, "distance between two locations = "+_strDistance, Toast.LENGTH_SHORT).show();
public static BigDecimal round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd;
}
Your approximation is right. It returns the distance in meters.
You can convert it to KM by dividing it by 1000 like so;
float distance = locationA.distanceTo(locationB)/1000;
Read more about distanceTo here.
here in my screenshot you can see, if we take location from double latitude, longitude i.e. 6 digit after decimal but actually Location hold latitude value till 8 digits. The main difference is here. So don't use double for storing latitude, longitude. Try to use anotherway

Moving GPS coordinate

I am fairly new to Android programming, but I am getting pretty good at it (I think: ))
What I am doing is building a situated stories app. It is an app that places audio files at certain GPS markers and enables the user to listen to them at specific locations.
The next step is moving audio files. What I want to do is set a marker at a specific position in a city. (done). Next I want to check the location of a second marker that moves in a circle around it.
What I have so far is this:
public void checkCircularPosition(){
/*
* could be a solution?
*
radius = 250; //offset in meters
gpsLatCenter = 5.1164; //?how can i make this accurate in meters?
gpsLonCenter = 52.0963; //??how can i make this accurate in meters?
degree = 0; //should be variable over time (full circle in 1Hr, 3600sec --> 360/3600 = 0,1 deg/s)
radian;
radian = (degree/180)*Math.PI;
gpsCircleLat = gpsLatCenter+Math.cos(radian)*radius;
gpsCircleLon = gpsLonCenter-Math.sin(radian)*radius;
*/
}
Now, I have checked this code in adobe flash, which made a movie clip move around in a circle. So I know the calculations are somewhat right. But, I have no way of calculating the latitude and longitude of the resulting coordinates.
EDIT!!
i found the solution with the help posted below. still a lot of work to figure out how to use the results. anyway, i posted the resulting function below.
to make this work, you need _radius wich is 6371 (earth's radius), a bearing, a distance, and a start location.
thanks a lot guys!
public static void destinationPoint(double brng, double dist) {
dist = dist/_radius; // convert dist to angular distance in radians
brng = Math.toRadians(brng); //
double lat1 = Math.toRadians(_lat);
double lon1 = Math.toRadians(_lon);
double lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist) + Math.cos(lat1)*Math.sin(dist)*Math.cos(brng) );
double lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(lat1), Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));
lon2 = (lon2+3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180ยบ
Log.i(APPTAG, ""+Math.toDegrees(lat2));
Log.i(APPTAG, ""+Math.toDegrees(lon2));
Location movLoc = new Location("");
movLoc.setLatitude(Math.toDegrees(lat2));
movLoc.setLongitude(Math.toDegrees(lon2));
Log.i(APPTAG, ""+movLoc);
}
You should check the section Destination point given distance and bearing from start point at this website: http://www.movable-type.co.uk/scripts/latlong.html
That website has the proper formula for using your start point (gpsLatCenter/gpsLonCenter) and bearing (degree in you code) to compute the final lat/lon (gpsCircleLat/gpsCircleLon).

Categories

Resources