我使用了两个按钮。有两个按钮获取当前位置。下一个按钮是一个开始按钮,当我点击时应该给出5米后当前位置和位置之间的距离
答案 0 :(得分:1)
如果您知道两个点的纬度很长,那么您可以按照以下方式计算这两个点之间的距离
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);
答案 1 :(得分:0)
使用以下api
GET http://maps.googleapis.com/maps/api/directions/json?origin="sourceLat,sourceLang"&destination="destLat,destLan"g&sensor=false
你最终会得到一些JSON。在路线[]。legs []。距离中,你会得到一个像这样的物体:
"legs" : [
{
"distance" : {
"text" : "542 km",
"value" : 542389
},
答案 2 :(得分:0)
使用此方法:
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);
}
答案 3 :(得分: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";
}
答案 4 :(得分:0)
请看下面的Url谷歌为大多数地图用例提供了实用程序库。
https://developers.google.com/maps/documentation/android-api/utility/#introduction
用于计算两个纬线长度之间的距离:
SphericalUtil.computeDistanceBetween(LatLng from, LatLng to);
答案 5 :(得分:0)
这样做
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