calculating distance showing wrong some time with latitude and longitude - android

I have calculated the distance between two places by using Latitude and longitude.Some times it showing wrong distance.I need to calculate exact distance in miles. Could you please help. what should i make changes in my code.
Here is my Code.
lat2 and lng2 are the current location geo points I have taken these by NETWORK PROVIDER/GPS.
lat1 and lng2 are the destination geo poin
double earthRadius = 3958.75;//earthRadius in miles
// double earthRadius = 6371;
double dLat = Math.toRadians(lat1-lat2);
double dLng = Math.toRadians(lng1-lng2);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(lat1)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;//distance in miles
Thanks you..

Try like this.
float results[] = new float[3];
Location.distanceBetween(latitude_a, longitude_a, latitude_b,longitude_b, results);
distance = results[0];

private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}

Related

An API for calculating a road distance

we develop a application that we need calculate road distance with two Geographical spots (lat & long) like google API and i cant use google API. please help us..thanks
You can calculate distance in kilometers by using following code:
private double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad)
{
return (rad * 180.0 / Math.PI);
}
double distance(double lat1, double lon1, double lat2, double lon2)
{
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1))
* Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1))
* Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}

Calculate the total travelled distance Google Maps API V3 in Android

Till now, I have inserted all the sets of latitude and longitude in my database after every 5 sec from the path I have travelled.
Now after reaching the destination I have to calculate the distance from the sets of Latitude, longitude. It's not about calculating the travelling distance between two points but to get the total distance travelled from set of lat,long in my database .
I have thought a way of calculating the total distance by getting the distance between the two points
and add them up at last
Is there any better solution than this? please suggest and help
Already read this SO post but not getting how I will get the total distance by using the Google Maps Distance Matrix API
///////service to insert coordinates into the database.
private class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
String name = arg1.getAction();
if (name.equalsIgnoreCase("Location_Changed")) {
db.insertlatlong(new Latilongi(arg1.getStringExtra("latitude"),
arg1.getStringExtra("longitude"),currentDateandTime));
} else if (arg1.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(MapActivity.this, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
}
}
}
you can use the following method that will give you acurate result
public double CalculationByDistance(LatLng StartP, LatLng EndP) {
int Radius = 6371;// radius of earth in Km
double lat1 = StartP.latitude;
double lat2 = EndP.latitude;
double lon1 = StartP.longitude;
double lon2 = EndP.longitude;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value", "" + valueResult + " KM " + kmInDec
+ " Meter " + meterInDec);
return Radius * c;
}
try this one
private double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == "K") {
dist = dist * 1.609344;
} else if (unit == "N") {
dist = dist * 0.8684;
}
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
You can insert only distance between two co-ordinates and lastly sum up all distances you had in database. Distance achieve like below.
float distanceInMeters = lastLocation.distanceTo(myLocation);
where "lastLocation" and "myLocation" are of Location object.
If you dont have Location object than you can try this.
Location lastLocation = new Location("");
lastLocation.setLatitude(latitude);
lastLocation.setLongitude(longitude);
Location myLocation = new Location("");
myLocation.setLatitude(otherlatitude);
myLocation.setLongitude(otherlongitude);
float distanceInMeters = lastLocation.distanceTo(myLocation);

Distance between from one point to another point on map with the help of Location

In My app,i am using map and want to get the distance between from one place to another place by the help of Location. and i am getting location of every point when change the location.
give source and destination lat long and calculate distance in km.
public static double getDistance(double lat1, double lon1, double lat2, double lon2) {
int R = 6371; // Radius of the earth in km
double dLat = deg2rad(lat2 - lat1); // deg2rad below
double dLon = deg2rad(lon2 - lon1);
double a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = R * c; // Distance in km
return d;
}
public static double deg2rad(double deg) {
return deg * (Math.PI / 180);
}

get distance between Current location and Selected Place in android

I am new to android developing, I want to make an application like give me distance on MapView between my current location and selected place.
please give me some suggestion for this.
Get the distance between current location and a selected place using android.
public static double distFrom(
double lat1, double lng1, double lat2, double lng2)
{
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
return dist;
}
Only a few lines of code:
Location l1=new Location("One");
l1.setLatitude(location.getLatitude());
l1.setLongitude(location.getLongitude());
Location l2=new Location("Two");
l2.setLatitude(Double.parseDouble(frnd_lat));
l2.setLongitude(Double.parseDouble(frnd_longi));
float distance_bw_one_and_two=l1.distanceTo(l2);

how to find the distance between two geopoints?

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);
the above code is not working and i am getting 0.0 Km as distance?
Also in the constructor of the location class, what does the string provider mean.
In the above code i am using PointA and PointB as the providers.
why is the above code not working?
thank you in advance.
LOGCAT
05-09 17:48:56.144: INFO/current loc(1573): lat 0.0 lng 0.0
05-09 17:48:56.155: INFO/checklocation loc(1573): lat 54.4288665 lng 10.169366
Just a quick snippet since I didn't see a complete and simple solution with GeoPoints above:
public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {
double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
float [] dist = new float[1];
Location.distanceBetween(lat1, lng1, lat2, lng2, dist);
return dist[0] * 0.000621371192f;
}
If you want meters, just return dist[0] directly.
Try Location.distanceBetween(..)
Update:
If you are getting lat/lon from GeoPoint then they are in microdegrees. You must multiply by 1e6.
As stated above, the Location Class is the way to go. Here is the code I have used :
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);
In this example, both pointA and pointB are instances of the GeoPoint class.
This is not an answer, but note the signature for the Location constructor is Location(String provider).
i.e. the String you pass to the constructor should be one of LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER or LocationManager.PASSIVE_PROVIDER, not "point A" and "point B".
An alternative way to accomplish the above,
public class Distance {
public static double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
//if (unit == "K") {
// dist = dist * 1.609344;
// else if (unit == "N") {
//dist = dist * 0.8684;
//}
return (dist);
}
public static final double PI = 3.14159265;
public static final double deg2radians = PI/180.0;
public static double getDistance(double latitude1, double longitude1, double latitude2,double longitude2) {
double lat1 = latitude1 * deg2radians;
double lat2 = latitude2 * deg2radians;
double lon1 = longitude1 * deg2radians;
double lon2 = longitude2 * deg2radians;
// Williams gives two formulae;
// this is the more accurate for close distances.
// In practice, the two differed only in the 8th or 9th place, for
// separations as small as 1 degree.
double radd = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2),
2.0)
+ Math.cos(lat1)
* Math.cos(lat2)
* Math.pow(Math.sin((lon1 - lon2) / 2), 2.0)));
return radd;
}
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts decimal degrees to radians : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts radians to decimal degrees : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private static double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
}
double CalculateDistance( double nLat1, double nLon1, double nLat2, double nLon2 )
{
double nRadius = 6371; // Earth's radius in Kilometers
// Get the difference between our two points
// then convert the difference into radians
double nDLat = ToRad(nLat2 - nLat1);
double nDLon = ToRad(nLon2 - nLon1);
// Here is the new line
nLat1 = ToRad(nLat1);
nLat2 = ToRad(nLat2);
double nA = pow ( sin(nDLat/2), 2 ) + cos(nLat1) * cos(nLat2) * pow ( sin(nDLon/2), 2 );
double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
double nD = nRadius * nC;
return nD; // Return our calculated distance
}
http://www.jaimerios.com/?p=39

Categories

Resources