计算地理位置距离返回 NaN

Calculation geolocation distance returns NaN

本文关键字:NaN 返回 距离 地理位置 计算      更新时间:2023-09-26

我正在尝试计算用户与以下坐标之间的距离,我不确定我做错了什么,但我得到响应"NaN"(不是数字)

.HTML:

<h3>Come visit</h3>
<p>You're only <span class="distance"> </span>km away</p>

JS文件:

// Location
if($('#content').hasClass('contact_page')) {
    var localSearch = new GlocalSearch();
    var sharp_hq = {
        lat: 53.639993,
        lng: -1.782354
    };
   // calculate the distance between me and thee
    var calculate_distance = function(location) {
        return Math.round(3959 * 1.609344 
       * Math.acos(Math.cos(0.0174532925 * location.y) 
       * Math.cos(0.0174532925 * sharp_hq.lat)
       * Math.cos((0.0174532925 * sharp_hq.lng) - (location.x * 0.0174532925))
       + Math.sin(0.0174532925 * location.y)
       * Math.sin(0.0174532925 * sharp_hq.lat)));
    };
    // geo location
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            //display in .distance <span>
            $('.distance', '.location').html(calculate_distance(initialLocation));
            $('p', '.location').css({ display: 'block' });
        });
    }
}
这对

我有用;

$(document).ready(function(){
    var sharp_hq = {
        lat: 53.639993,
        lng: -1.782354
    };
    // geo location
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            $('.distance').html( gc(position.coords.latitude,position.coords.longitude,sharp_hq.lat,sharp_hq.lng).toFixed(2) );
            $('p', '.location').css({ display: 'block' });
        });
    }
} ) ;
/** Converts numeric degrees to radians */
if (typeof (Number.prototype.toRad) === "undefined") {
    Number.prototype.toRad = function () {
        return this * (Math.PI / 180);
    }
};
function gc(lat1, lon1, lat2, lon2) {
    // returns the distance in km between the pair of latitude and longitudes provided in decimal degrees
    var R = 6371; // km
    var dLat = (lat2 - lat1).toRad();
    var dLon = (lon2 - lon1).toRad();
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                    Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
                    Math.sin(dLon / 2) * Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;
    return d;
}

google.maps.LatLng 对象没有 .x 或 .y 属性

看起来您可能仍然期望Google Maps Javascript API v2能够工作(它已被v3的包装器所取代)。

使用 location.lat() 代替 location.x/location.y 作为纬度,使用 location.lng() 作为经度。 或者使用.x和.y属性创建自己的匿名对象(但不要指望它可以与google.maps.LatLng对象互换)。

注意:Google Maps Javascript API v3 有一个用于计算距离的库。

您可以使用名为 google.maps.geometry.computeDistanceBetween() 的函数来查找两个 latlng 对象之间的距离。

https://developers.google.com/maps/documentation/javascript/geometry

将对 calculate_distance 函数的调用替换为

var distance = google.maps.geometry.spherical.computeDistanceBetween(
  new google.maps.LatLng(sharp_hq.lat, sharphq.lng),
  initialLocation
) / 1000;
$('.distance').text(distance);

返回以米为单位的距离,因此除以 1000。

在您的公式中,(0.0174532925 * sharp_hq.lng) - (location.x * 0.0174532925) 的值可能<0 导致 NaN。

如果它为 <0,则需要将其预处理为 0 以避免异常。

我希望这可以在不更改整个公式的情况下解决您的问题。