我一直在學習由Nikil Abraham編碼爲傻瓜編碼。 在這本書中有一個項目,其中包含的代碼用於開發一個餐廳的應用程序,在點擊登記按鈕時,餐廳的優惠券在餐廳步行5至10分鐘內向客戶發送警報。警報問題 - 使用Harvesine距離公式的地理位置公式
我輸入了所有使用html,css和javascript通過codepen的代碼,並且由於某種原因警報不會彈出。
我研究了哪裏可能會出錯,但我真的被卡住了。
下面是代碼:
在HTML:
<head>
<title>McDuck's App</title>
</head>
<body>
<h1>McDuck's Local Offers</h1>
<button onclick="getLocation()">CheckIn</button>
<div id="geodisplay"/>
<div id="effect"/>
</body>
的CSS:
body {
text-align: center;
background: white;
}
h1, h2, h3, p {
font-family: Sans-Serif;
color: black;
}
p {
font-size: 1em;
}
的JavaScript:
function getLocation() {
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showLocation);
}
}
//商店位置
function showLocation(position){
var mcduckslat=53.510207;
var mcduckslon=-6.399289;
//當前位置
var currentpositionlat=position.coords.latitude;
var currentpositionlon=position.coords.longitude;
//計算當前的位置和McDuck的位置之間的距離
var distance=getDistanceFromLatLonInMiles(mcduckslat, mcduckslon,
currentpositionlat, currentpositionlon);
//displays the locaton using .inner.HTML property and the lat & long
coordinates from your current location
document.getElementById("geodisplay").inner.HTML="Latitude: " +
currentpositionlat + "<br>Longitude: " + currentpositionlon;
}
//haversine distance formula
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
var R = 6371; //Radius of earth in km
var dLat = deg2rad(lat2-lat1);
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) *
Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a),
Math.sqrt(1-a));
var d = R * c * 0.372823; //Distance in miles
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180);
}
alert(distance);
if (distance < 0.5) {
alert("You get a free meal");
}
else {
alert("Thanks for checking in!");
}
鏈接到copepen http://codepen.io/Saharalara/pen/Grxmmj
我的回答對你有幫助嗎?點擊我答案頂部的鏈接查看解決方案的工作情況。 – taylorsabell