1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 高德地图获取坐标距离_计算两个坐标点之间的距离(高德地图)

高德地图获取坐标距离_计算两个坐标点之间的距离(高德地图)

时间:2021-11-24 08:37:28

相关推荐

高德地图获取坐标距离_计算两个坐标点之间的距离(高德地图)

/**

* 计算两点的距离

*

* @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;

}

标签:toPoint,distance,double,fromPoint,地图,坐标,高德,Math,1000

来源: /pccywq/p/12876253.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。