var earth_radius=6378.137;
// 计算两个脑残点之间的距离, 单位 : 公里
function get_map_distance(latA,lngA,latB,lngB){
var a=latA*Math.PI/180.0-latB*Math.PI/180.0;
var b=lngA*Math.PI/180.0-lngB*Math.PI/180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(latA*Math.PI/180.0) * Math.cos(latB*Math.PI/180.0) * Math.pow(Math.sin(b / 2), 2)));
return s * earth_radius;
}
// 计算两个脑残点之间的夹角, 单位 : 度
function get_map_bearing(lat1,lng1,lat2,lng2){
var dLon = (lng2-lng1)* Math.PI / 180;
var rlat1 = lat1* Math.PI / 180;
var rlat2 = lat2* Math.PI / 180;
var y = Math.sin(dLon) * Math.cos(rlat2);
var x = Math.cos(rlat1)*Math.sin(rlat2) - Math.sin(rlat1)*Math.cos(lat2)*Math.cos(dLon);
var rad = Math.atan2(y, x);
var brng = rad* 180 / Math.PI;
return (brng + 360) % 360;
}
// 根据距离和夹角,启示点, 在地图上找脑残点
function get_map_poi_distance(lat1,lng1,distance,bearing){
var brng = bearing* Math.PI / 180;
var rlat1 = lat1* Math.PI / 180;
var rlng1 = lng1* Math.PI / 180;
var dist = distance/earth_radius;
var lat2 = Math.asin( Math.sin(rlat1)*Math.cos(dist) +
Math.cos(rlat1)*Math.sin(dist)*Math.cos(brng) );
var lon2 = rlng1 + Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(rlat1),
Math.cos(dist)-Math.sin(rlat1)*Math.sin(lat2));
lon2 = (lon2+3*Math.PI) % (2*Math.PI) - Math.PI;
return {
lat : (lat2* 180 / Math.PI),
lng : (lon2* 180 / Math.PI)
}
}
// 获得两个脑残点的中间脑残点GPS坐标, 绝对不是两个加一下除以2哦!!!
function get_map_midpoint(lat1,lng1,lat2,lng2){
var dLon = (lng2-lng1)* Math.PI / 180;
var rlat1 = lat1* Math.PI / 180;
var rlat2 = lat2* Math.PI / 180;
var rlng1 = lng1* Math.PI / 180;
var Bx = Math.cos(rlat2) * Math.cos(dLon);
var By = Math.cos(rlat2) * Math.sin(dLon);
var lat3 = Math.atan2(Math.sin(rlat1)+Math.sin(rlat2),
Math.sqrt( (Math.cos(rlat1)+Bx)*(Math.cos(rlat1)+Bx) + By*By ) );
var lon3 = rlng1 + Math.atan2(By, Math.cos(rlat1) + Bx);
return {
lat : (lat3* 180 / Math.PI),
lng : (lon3* 180 / Math.PI)
}
}
时间: 2024-10-12 07:54:11