代码之家  ›  专栏  ›  技术社区  ›  rob.m

如何检查坐标是否在以公里为单位的半径范围内?

  •  1
  • rob.m  · 技术社区  · 5 年前

    给出了一个 json 具有 坐标 :

    var centerLat = 51.6000;
    var centerLng = 12.8000;
    
    var posts = [
      {
        name: 'PostA',
        latitude: '52.5167',
        longitude: '13.3833',
      },
      {
        name: 'PostB',
        latitude: '52.9667',
        longitude: '13.7167',
      },
      {
        name: 'PostC',
        latitude: '26.7767',
        longitude: '18.4567',
      }
    ];
    

    我找到了 哈维辛 公式 this link ,如何检查 lat lng 我从 杰森 在A内 radius 属于 5公里 使用 哈维辛 是吗?

    function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
      var R = 6371; // Radius of the earth in km
      var dLat = deg2rad(lat2-lat1);  // deg2rad below
      var dLon = deg2rad(lon2-lon1); 
      var a = 
        Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
        Math.sin(dLon/2) * Math.sin(dLon/2)
        ; 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      var d = R * c; // Distance in km
      return d;
    }
    
    function deg2rad(deg) {
      return deg * (Math.PI/180)
    }
    
    2 回复  |  直到 5 年前
        1
  •  2
  •   Nils Kähler    5 年前

    你可以这样做来过滤城市,你提供的数据离得太远,以得到我移动的结果中的任何东西。 centerLat centerLng 我已经记录了最靠近城市的阵列。

    function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
      var R = 6371; // Radius of the earth in km
      var dLat = deg2rad(lat2 - lat1); // deg2rad below
      var dLon = deg2rad(lon2 - lon1);
      var a =
        Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
        Math.sin(dLon / 2) * Math.sin(dLon / 2);
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
      var d = R * c; // Distance in km
      return d;
    }
    
    function deg2rad(deg) {
      return deg * (Math.PI / 180)
    }
    
    var centerLat = 52.5167;
    var centerLng = 13.3933;
    
    var posts = [{
        name: 'PostA',
        latitude: '52.5167',
        longitude: '13.3833',
      },
      {
        name: 'PostB',
        latitude: '52.9667',
        longitude: '13.7167',
      },
      {
        name: 'PostC',
        latitude: '26.7767',
        longitude: '18.4567',
      }
    ];
    
    let closePosts = [];
    
    posts.forEach((post) => {
      if (getDistanceFromLatLonInKm(centerLat, centerLng, post.latitude, post.longitude) < 5) {
        closePosts.push(post);
      }
    });
    
    console.log(closePosts)
        2
  •  1
  •   manonthemat    5 年前

    只需将它添加到代码中就足够了。

    posts.forEach(post => {
      const distance = getDistanceFromLatLonInKm(centerLat, centerLng, post.latitude, post.longitude);
      if (distance <= 200) {
        console.log(`Distance to ${post.name}: ${distance} km`);
      }
    });
    

    但我建议你多清理一下代码。我使用了200而不是5,因为您的示例不会产生结果。

    这是一个重构的片段。

    const posts = [
      {
        name: 'Berlin',
        latitude: '52.520008',
        longitude: '13.404954',
      },
      {
        name: 'Hamburg',
        latitude: '53.551086',
        longitude: '9.993682',
      },
      {
        name: 'München',
        latitude: '48.135124',
        longitude: '11.581981',
      },
      {
        name: 'Lübeck',
        latitude: '53.865467',
        longitude: '10.686559',
      },
      {
        name: 'Schwerin',
        latitude: '53.635502',
        longitude: '11.401250',
      },
    ];
    
    function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
      const R = 6371; // Radius of the earth in km
      const dLat = deg2rad(lat2-lat1); // deg2rad below
      const dLon = deg2rad(lon2-lon1);
      const a =
            Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
            Math.sin(dLon/2) * Math.sin(dLon/2)
          ;
      const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
      const d = R * c; // Distance in km
      return d;
    }
    
    function deg2rad(deg) {
      return deg * (Math.PI/180);
    }
    
    function findClosePosts(location, radius, posts) {
      return posts.filter((post) =>
        // find close points within the radius of the location, but exclude the location itself from results
        getDistanceFromLatLonInKm(location.latitude, location.longitude, post.latitude, post.longitude) <= radius && location !== post);
    }
    
    function findLocationByName(name, posts) {
      return posts.find((post) => post.name === name);
    }
    
    const hamburg = findLocationByName('Hamburg', posts);
    const closePosts = findClosePosts(hamburg, 200, posts);
    console.log(closePosts);