2013-12-17 86 views
1

我正在嘗試計算用戶和下方座標之間的距離,我不確定我做了什麼錯誤,但是我得到的響應'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' }); 
     }); 
    } 
} 
+0

initialLocation的值是否爲預期值? –

回答

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; 
} 
+0

這很好,謝謝 – DLaverick

1

google.maps.LatLng對象不具有.x或.Y屬性

看起來你可能仍然期待着Google Maps Javascript API v2的工作(它已被替換爲v3的包裝)。

而不是location.x/location.y使用緯度和location.lng()的location.lat()作爲經度。或者使用.x和.y屬性創建自己的匿名對象(但不要期望它可以與google.maps.LatLng對象互換)。

注意: Google Maps JavaScript API v3有library to calculate distance

0

您可以使用名爲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

這是如何解決問題的? – geocodezip

+0

它計算用戶的位置和另一個點之間的距離。 – js1568

0

在您的公式,價值(0.0174532925 * sharp_hq.lng) - (location.x * 0.0174532925)可能是< 0引起的NaN。

如果是< 0,則需要將其預處理爲0以避免發生異常。

我希望這可以解決您的問題,而無需更改整個公式。