Reputation: 7
I have used two buttons.one two get the current location. Next button is a start button which when I click should give the distance between current location and location after 5 metres
Upvotes: -1
Views: 93
Reputation: 1354
Do This Way
double distance;
Location locationA = new Location("Kaaba");
locationA.setLatitude(21.45);
locationA.setLongitude(-39.75);
Location locationB = new Location("My Location");
locationB.setLatitude("17.20");
locationB.setLongitude("73.12");
distance = locationA.distanceTo(locationB) / 1000; //in Km
Upvotes: 0
Reputation: 1678
Look at bellow Url google provides utility library for most of the Maps use case.
https://developers.google.com/maps/documentation/android-api/utility/#introduction
For calculating distance between two lat longs:
SphericalUtil.computeDistanceBetween(LatLng from, LatLng to);
Upvotes: 0
Reputation: 7536
If you know lat long for both point then you can calculate distance between those two points in following way
double distance;
Location oldlocation = new Location("");
oldlocation.setLatitude(main_Latitude);
oldlocation.setLongitude(main_Longitude);
Location newlocation = new Location("");
newlocation.setLatitude(sub_Latitude);
newlocation.setLongitude(sub_Longitude);
distance = oldlocation.distanceTo(newlocation);
Upvotes: 1
Reputation: 4182
String currentlat="";// put the lat
String currentlong="";// put the long
String str_lat=""; //put the destination lat or last point to get the distance
String str_lng="";//put the destination longtitude
int Radius = 6371;// radius of earth in Km
double lat1 = Double.valueOf(Str_userlat);
double lat2 = Double.valueOf(str_lat);
double lon1 = Double.valueOf(Str_userlng);
double lon2 = Double.valueOf(str_lng);
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);
String str_location="0";
if(kmInDec==0)
{
tvLocation.setText("<"+String.valueOf(meterInDec)+" m");
str_location=String.valueOf(meterInDec)+" m";
}
else if(kmInDec==0&&meterInDec==0)
{
tvLocation.setText("<"+String.valueOf(meterInDec)+" m");
str_location=String.valueOf(meterInDec)+" m";
}
else
{
tvLocation.setText("<"+String.valueOf(kmInDec)+" kM");
str_location=String.valueOf(kmInDec)+" km";
}
Upvotes: 0
Reputation: 640
Use This method:
public double distance(double lat1, double lon1, double lat2, double lon2, char 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);
}
Upvotes: 0
Reputation: 3195
Use following api
GET http://maps.googleapis.com/maps/api/directions/json?origin="sourceLat,sourceLang"&destination="destLat,destLan"g&sensor=false
You'll end up with some JSON. In routes[].legs[].distance, you'll get an object like this:
"legs" : [
{
"distance" : {
"text" : "542 km",
"value" : 542389
},
Upvotes: 0