【问题标题】:Calculation geolocation distance returns NaN计算地理位置距离返回 NaN
【发布时间】:2014-01-05 14:08:52
【问题描述】:

我正在尝试计算用户与下面坐标之间的距离,我不确定我做错了什么,但我得到了响应“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' });
        });
    }
}

【问题讨论】:

  • initialLocation 的值是预期值吗?

标签: javascript jquery google-maps-api-3 geolocation


【解决方案1】:

这对我有用;

$(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;
}

【讨论】:

    【解决方案2】:

    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 有一个library to calculate distance

    【讨论】:

      【解决方案3】:

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

      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。

      【讨论】:

      • 计算用户位置与另一点之间的距离。
      【解决方案4】:

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

      如果为

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        • 1970-01-01
        • 2021-04-19
        相关资源
        最近更新 更多