在地图上查找使用谷歌地图API V2两点之间的距离
问题描述:
我使用谷歌地图API V2在我的Android应用程序,我能够显示地图,并把标记就可以了,但现在我坚持的,我需要找出两个标记或点之间的距离问题放置在地图上,我已经通过文档走了,但没有发现任何有用的在这种情况下。
I am using google map api v2 in my android application, I am able to show the map and put markers on it, but now I am stuck with the problem in which I need to find out the distance between two markers or points placed on map, I have already gone through the docs but didn't find anything helpful in this case.
如果有谁知道如何解决这个问题,请您帮助我。
If anyone knows how to solve this then please help me.
感谢
答
您可以使用,这将使你acurate的结果下面的方法
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;
}