如何计算与距离

时间:2016-06-23 07:15:13

标签: javascript gis

我是gis的新手。我在使用JavaScript。

假设有中心latlong。另一点是目标(latlong)远离中心的n米。如何计算这个位置差异(中心 - latlong)从中心和n在斜切。

伙计们请帮助我。

让我们认为地球是正确的球形。

2 个答案:

答案 0 :(得分:1)

  function distance(lat1, lon1, lat2, lon2) {
    var radius = 6371e3; // meters
    var dLon = gis.toRad(lon2 - lon1),
        lat1 = gis.toRad(lat1),
        lat2 = gis.toRad(lat2),
        distance = Math.acos(Math.sin(lat1) * Math.sin(lat2) +
            Math.cos(lat1) * Math.cos(lat2) * Math.cos(dLon)) * radius;
    return distance;
  }

以上javascript函数用于计算距离,

function(lat, long, distance){
  //can i calculate lat_difference, long difference
}

答案 1 :(得分:1)

这是你的代码: -

<script>

Math.radians = function(degrees) {
  return degrees * Math.PI / 180;
};


function calculateDistance(lat,lon,lat_center,lon_center){

var distance = ( 6371 * Math.acos( Math.cos( Math.radians(lat) ) * Math.cos( Math.radians( lat_center ) ) 
                        * Math.cos( Math.radians( lon_center ) - Math.radians(lon) ) + Math.sin( Math.radians(lat) ) * Math.sin(Math.radians(lat_center)) ) )*1000;
console.log(distance+"  meter");
return distance;
}


var lat = '38.898556';
var lon = '-77.037852';

var lat_center = '38.897147';
var lon_center = '-77.043934';

calculateDistance(lat,lon,lat_center,lon_center);//will return 549 meter

//for getting lat and lon from a distance from a given point

//lat1 = latitude of start point in degrees

//long1 = longitude of start point in degrees

//d = distance in KM

//angle = bearing in degrees

function get_gps_distance(lat1,long1,d,angle)
{
    //# Earth Radious in KM
    var R = 6378.14;

    //# Degree to Radian
    var latitude1 = lat1 * (Math.PI/180);
    var longitude1 = long1 * (Math.PI/180);
    brng = angle * (Math.PI/180);

    latitude2 = Math.asin(Math.sin(latitude1)*Math.cos(d/R) + Math.cos(latitude1)*Math.sin(d/R)*Math.cos(brng));
    longitude2 = longitude1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(latitude1),Math.cos(d/R)-Math.sin(latitude1)*Math.sin(latitude2));

   // # back to degrees
    latitude2 = latitude2 * (180/Math.PI);
    longitude2 = longitude2 * (180/Math.PI);

   // # 6 decimal for Leaflet and other system compatibility
   lat2 = latitude2;
   long2 =longitude2;
var tab = {};
   // Push in array and get back
   tab[0] = lat2;
   tab[1] = long2;
   return tab;
 }

 get_gps_distance('38.898556','-77.037852',.549,90);

</script>