计算地理位置距离返回 NaN
Calculation geolocation distance returns NaN
我正在尝试计算用户与以下坐标之间的距离,我不确定我做错了什么,但我得到响应"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 以避免异常。
我希望这可以在不更改整个公式的情况下解决您的问题。
相关文章:
- 如何防止Math.max()返回NaN
- 尝试求和时,在我的表的页脚中返回$NaN
- 为什么Math.pow(1,无穷大)返回NaN
- JavaScript:返回NaN(第2部分)
- 为什么这个代码返回NaN
- Protractor-分配给element.all.locator.count的变量返回NaN why
- javascript 在求和时返回 NaN
- 来自函数 Javascript 的 NaN 返回值 ||函数执行顺序
- 返回“[对象对象] NaN” - Javascript
- 返回为 NaN 的值
- 计算地理位置距离返回 NaN
- Javascript函数返回'NaN'或者'未定义'
- nan在C++中是什么意思?为什么pow(-4,-2.1)返回-nan
- 货币字段上的 parseInt 返回 NaN
- Javascript:解析csv中的字符串返回NaN(不是数字)
- JS脚本返回NaN
- 我如何才能得到返回'0'而不是NaN
- 解析html td返回NaN
- Javascript函数返回NaN
- 为什么angular.isNumber(NaN)返回true ?