/**
* 计算两点的距离
*
* @param fromPoint
* @param toPoint
* @return 返回String类型带距离单位
*/
public static String measureDistanceStr(LatLng fromPoint, LatLng toPoint) {
String distanceStr = "";
if (fromPoint != null && toPoint != null) {
long distance = MapUtil.measureDistance(fromPoint, toPoint);
if (distance >= 1000) {
if (distance % 1000 == 0) {
distanceStr = distance / 1000 + "km";
} else {
if ((distance % 1000 + "").length() < 3) {
distanceStr = distance / 1000 + ".0km";
} else {
long l1 = (distance % 1000) / 100;
distanceStr = distance / 1000 + "." + l1 + "km";
}
}
} else {
distanceStr = distance + "m";
}
}
return distanceStr;
}
//测量两点的距离
public static long measureDistance(LatLng fromPoint, LatLng toPoint) {
double EARTH_RADIUS = 6378137;
long distance = 0;
double startLongitude = fromPoint.longitude;
double startLatitude = fromPoint.latitude;
double endLongitude = toPoint.longitude;
double endLatitude = toPoint.latitude;
double radLatitude1 = startLatitude * Math.PI / 180.0;
double radLatitude2 = endLatitude * Math.PI / 180.0;
double a = Math.abs(radLatitude1 - radLatitude2);
double b = Math.abs(startLongitude * Math.PI / 180.0 - endLongitude * Math.PI / 180.0);
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLatitude1) * Math.cos(radLatitude2) * Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
distance = Math.round(s * 10000) / 10000; // 返回距离单位是米
return distance;
}