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
Asked
Active
Viewed 86 times
-1
-
Thank you for your answers. If we are using two buttons then how will we implement here. I have created two methods.please give the complete code – claris Apr 19 '16 at 07:04
-
Duplicate https://stackoverflow.com/questions/8049612/calculating-distance-between-two-geographic-locations – General Grievance Aug 29 '23 at 15:21
6 Answers
1
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);

Jhaman Das
- 1,094
- 13
- 28

nilkash
- 7,408
- 32
- 99
- 176
0
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
},

mdDroid
- 3,135
- 2
- 22
- 34
0
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);
}

kalidoss rajendran
- 640
- 4
- 13
0
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";
}

Arjun saini
- 4,223
- 3
- 23
- 51
0
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);

Kalpesh Patel
- 1,638
- 1
- 20
- 35
0
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

Bajirao Shinde
- 1,356
- 1
- 18
- 26