Ich habe zwei buttons.one zwei verwendet den aktuellen Standort. Schaltfläche Weiter ist eine Starttaste, die beim Klicken sollte der Abstand zwischen der aktuellen Lage und Lage nach 5 MeterWie finde ich den Abstand zwischen zwei Punkten mit Breiten- und Längengraden in Android
Antwort
Verwenden folgenden api
GET http://maps.googleapis.com/maps/api/directions/json?origin="sourceLat,sourceLang"&destination="destLat,destLan"g&sensor=false
Sie geben werde mit einigen JSON enden. .. In Routen [] Beine [] Abstand, werden Sie ein Objekt wie diese:
"legs" : [
{
"distance" : {
"text" : "542 km",
"value" : 542389
},
Wenn Sie lat lange für beiden Punkte kennen, dann können Sie Abstand zwischen diesen beiden Punkten berechnen, die in folgenden Weise
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);
Verwendung Diese Methode:
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);
}
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";
}
haben Sie irgendein Problem mit diesem Code? –
Schauen Sie sich unten URL google bietet Utility-Bibliothek für die meisten der Karten Fall verwenden.
https://developers.google.com/maps/documentation/android-api/utility/#introduction
Für Abstand zwischen zwei lat longs Berechnung:
SphericalUtil.computeDistanceBetween(LatLng from, LatLng to);
Sie auf diese Weise
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
Dank für Ihre Antworten danken. Wenn wir zwei Tasten verwenden, wie werden wir das hier umsetzen? Ich habe zwei Methoden erstellt. Bitte geben Sie den vollständigen Code – claris
akzeptieren meine Antwort, so dass die Frage geschlossen wird @claris –