如何检查当前 lat/long 是否在给定 lat/long 的 20 公里范围内

How to Check if current lat/long is within 20 KM of given lat/long

我有一个当前位置 lat/long 我有供应商位置的 lat/long

如果我的当前位置 lat/long 在供应商位置 lat/long

的 20 公里以内,我该如何检查
jQuery(document).on('click', '.add_to_cart_button', function(event) {
    event.preventDefault();

    var cur_href = jQuery(this).attr("href");
    var cur_pro_id = cur_href.substring(cur_href.indexOf('=') +1);


    var params =  {"cur_pro_id":cur_pro_id,action:"get_vendor_latlng"}

    jQuery.post(customization.ajaxurl,params,function(data){
        var vendor_data =  JSON.parse(data);
        console.log(vendor_data);
        if ( vendor_data.pro_ven_lat !== '' && vendor_data.pro_ven_lng !== '' ) {
            navigator.geolocation.getCurrentPosition(
                function( position ) { 
                    var cur_lat = position.coords.latitude;
                    var cur_lng = position.coords.longitude;

                }
            );
        } else {

        }

    });

});

此处 vendor_data.pro_ven_latvendor_data.pro_ven_lng 变量保存供应商位置的 lat/long

cur_latcur_lng变量保存当前位置的lat/long

您可以使用此函数获取距离 两者之间的公里数:

function getDistanceInKM (lat1, lon1, lat2, lon2) {
  if(lat1 === lat2 && lon1 === lon2) return 0;
  const 
    radlat1 = Math.PI * lat1/180,
    radlat2 = Math.PI * lat2/180,
    theta = lon1 - lon2;
  const radtheta = Math.PI * theta/180;
  let dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
  if (dist > 1) dist = 1;
  dist = Math.acos(dist);
  dist = dist * 180/Math.PI;
  dist = dist * 60 * 1.1515;
  dist = dist * 1.609344;
  return dist;
}  

源代码来自 here.