可能重複:
toRad() javascript function throwing error地理定位功能不起作用! JavaScript的
所以首先我有這兩個p標籤
<p id="pos"></p>
<p id="dist"></p>
如果我把 「POS」 我的位置和距離我之間和「dist」中的紐約。 我得到了這兩個按鈕來觸發JS功能:
<button onclick="getLocation()">Get position</button>
<button onclick="calcDistance()">Calculate distance to NY</button>
這裏是所有的腳本:
var x=document.getElementById("pos");
var d=document.getElementById("dist");
var startPos;
function getLocation(){
navigator.geolocation.getCurrentPosition(function(position) {
startPos = position;
x.innerHTML= "<h3>Your position</h3>" +
"Latitude: " + startPos.coords.latitude +
"<br>Longitude: " + startPos.coords.longitude;
});
};
function calcDistance(){
var distt = calculateDistance(startPos.coords.latitude, startPos.coords.longitude, 40.28371627, -73.994901);
d.innerHTML = distt.toFixed(1);
};
function calculateDistance(lat1,lon1,lat2,lon2) {
var R = 6371;
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));
return R * c;
}
我calcDistance按鈕沒有工作,我沒有一個線索是什麼問題是。 我試着只把數字放在calculateDistance()中,但它仍然不起作用。怎麼來的?
因爲'.toRad()'不是數字的號碼功能? – apsillers
我們的老師要求我們使用這個函數,所以我認爲我們不會在意它。那麼爲什麼toRad使它無法工作? –
你在調用一個不存在的函數。也許你的老師要求你使用定義'toRad'的庫?或者,也許你的老師希望你使用具有'toRad'功能的語言?或者你的老師只是希望你自己定義它。 – apsillers