单击按钮时,Javascript距离计算器不工作

时间:2015-04-24 01:13:21

标签: javascript jquery html css

试图让距离计算器在我的网页上运行时遇到问题。如果您可以查看下面的代码并看到任何错误,请告诉我。提前谢谢!

这是我的DistanceCalc.js文件:

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  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; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}

我的内联JS脚本:

/*----LOCATION FINDER SCRIPT----*/

<script src = "distancecalc.js"></script>
<script>
    function success(position) {
    document.getElementById ('visitus').style.display = 'block';

    document.getElementById ('coordlat').innerHTML =  position.coords.latitude;
    document.getElementById ('coordlong').innerHTML = position.coords.longitude;

    document.getElementById ('distance2').innerHTML = getDistanceFromLatLonInKm(51.897871900000000000, -8.471087399999988000, 
    position.coords.latitude, position.coords.longitude);
    }
</script>

HTML代码:

<p>To See Your Distance From <br>Clonakilty, Main Street, Click <button onclick="navigator.geolocation.getCurrentPosition(success);">Here</button></p>

以前从未在我的网站上使用过这类东西所以我真的不知道如何调试它。

0 个答案:

没有答案