百度地图测距js代码:
var EARTHRADIUS = 6370996.81;
/**
* 将度转化为弧度
* @param {degree} Number 度
* @returns {Number} 弧度
*/
GeoUtils.degreeToRad = function(degree){
return Math.PI * degree/180;
}
/**
* 将弧度转化为度
* @param {radian} Number 弧度
* @returns {Number} 度
*/
GeoUtils.radToDegree = function(rad){
return (180 * rad) / Math.PI;
}
/**
* 将v值限定在a,b之间,纬度使用
*/
function _getRange(v, a, b){
if(a != null){
v = Math.max(v, a);
}
if(b != null){
v = Math.min(v, b);
}
return v;
}
/**
* 将v值限定在a,b之间,经度使用
*/
function _getLoop(v, a, b){
while( v > b){
v -= b - a
}
while(v < a){
v += b - a
}
return v;
}
/**
* 计算两点之间的距离,两点坐标必须为经纬度
* @param {point1} Point 点对象
* @param {point2} Point 点对象
* @returns {Number} 两点之间距离,单位为米
*/
GeoUtils.getDistance = function(point1, point2){
//判断类型
if(!(point1 instanceof BMap.Point) ||
!(point2 instanceof BMap.Point)){
return 0;
}
point1.lng = _getLoop(point1.lng, -180, 180);
point1.lat = _getRange(point1.lat, -74, 74);
point2.lng =
point2.lat = _getRange(point2.lat, -74, 74);
var x1, x2, y1, y2;
x1 = GeoUtils.degreeToRad(point1.lng);
y1 = GeoUtils.degreeToRad(point1.lat);
x2 = GeoUtils.degreeToRad(point2.lng);
y2 = GeoUtils.degreeToRad(point2.lat);
return EARTHRADIUS * Math.acos((Math.sin(y1) * Math.sin(y2) + Math.cos(y1) * Math.cos(y2) * Math.cos(x2 - x1)));
}
/**
* 计算折线或者点数组的长度
* @param {Polyline|Array<Point>} polyline 折线对象或者点数组
* @returns {Number} 折线或点数组对应的长度
*/
GeoUtils.getPolylineDistance = function(polyline){
//检查类型
if(polyline instanceof BMap.Polyline ||
polyline instanceof Array){
//将polyline统一为数组
var pts;
if(polyline instanceof BMap.Polyline){
pts = polyline.getPath();
} else {
pts = polyline;
}
if(pts.length < 2){//小于2个点,返回0
return 0;
}
//遍历所有线段将其相加,计算整条线段的长度
var totalDis = 0;
for(var i =0; i < pts.length - 1; i++){
var curPt = pts[i];
var nextPt = pts[i + 1]
var dis = GeoUtils.getDistance(curPt, nextPt);
totalDis += dis;
}
return totalDis;
} else {
return 0;
}
}
大概方法即以上方法。这里对方法进行改写。
写成java形式
package com.duduli.li.gps2baidu;
public class Point {
private double x;
private double y;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
package com.duduli.li.gps2baidu;
import java.util.ArrayList;
import java.util.List;
public class Distacst {
private double EARTHRADIUS = 6370996.81;
public double degreeToRad(double degree){
return Math.PI*degree/180;
}
public double _getRange(double v, int a, int b){
if(a != 0){
v = Math.max(v, a);
}
if(b != 0){
v = Math.min(v, b);
}
return v;
}
public double _getLoop(double v, int a, int b){
while( v > b){
v -= b - a;
}
while(v < a){
v += b - a;
}
return v;
}
public double getDis(List<Point> list){
int size = list.size();
double dis = 0.0;
for(int i=0;i<size-1;i++){
dis += EARTHRADIUS * Math.acos((Math.sin(degreeToRad(_getLoop(list.get(i).getY(), -74, 74))) *
Math.sin(degreeToRad(_getLoop(list.get(i+1).getY(), -74, 74))) +
Math.cos(degreeToRad(_getLoop(list.get(i).getY(), -74, 74))) *
Math.cos(degreeToRad(_getLoop(list.get(i+1).getY(), -74, 74))) *
Math.cos(degreeToRad(_getLoop(list.get(i).getX(), -180, 180)) - degreeToRad(_getLoop(list.get(i+1).getX(), -180, 180)))));
}
return dis;
}
public static void main(String[] args) {
List<Point> list = new ArrayList<Point>();
Point p1 = new Point();
p1.setX(116.442953);
p1.setY(39.982648);
list.add(p1);
Point p2 = new Point();
p2.setX(116.444396);
p2.setY(39.982521);
list.add(p2);
Point p3 = new Point();
p3.setX(116.44574800000001);
p3.setY(39.982348);
list.add(p3);
System.out.println(new Distacst().getDis(list));
// System.out.println(new Distacst().getDis());
// System.out.println(new Distacst().showList(getList()));
}
}
传入参数为list。先算出两点之间的距离。然后进行累加。
如现在有3个点,那么先算1,2距离,然后算2,3距离。相加即得到结果。
以下是验证: