我有一个我声明的预先定位的位置。我从地理位置的二级位置。然后我尝试用标准公式计算距离。我不能让它输出距离......我做错了什么?对不起noob程序员在这里...
<!DOCTYPE html>
<html>
<body>
<p id="demo"> Distance calculations </p>
<button onclick="getLocation()"> Please Work</button>
<script>
var x=document.getElementById("demo");
var lat2= 40.084361;
var lng2= -83.100484;
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="geolocation is not supported";}
}
function showPosition(position)
{
x.innerHTML="Latitide: "+ position.coords.latitude +
"<br>Longitude: " + position.coords.longitude; }
</script>
<script>c
var newlat = position.coords.latitude;
var newlng = position.coords.longitude;
var R = 6371; km
var dLat = (newlat-lat2).toRad();
var dLon = (newlng-lng2).toRad();
var lat2 = lat2.toRad();
var newlat = newlat.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2)+
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat2)*Math.cos(newlat);
var c = 2* Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R *c;
x.innerHTML="distance:"+d);
</script>
答案 0 :(得分:0)
您有几个JavaScript错误。以下是应该有效的代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo"> Distance calculations </p>
<button onclick="getLocation()"> Please Work</button>
<script type="text/javascript">
var x=document.getElementById("demo");
var lat2= 40.084361;
var lng2= -83.100484;
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="geolocation is not supported";}
}
function showPosition(position)
{
x.innerHTML="Latitide: "+ position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
var newlat = position.coords.latitude;
var newlng = position.coords.longitude;
var R = 6371; //km
var dLat = (newlat-lat2).toRad();
var dLon = (newlng-lng2).toRad();
var lat2 = lat2.toRad();
var newlat = newlat.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2)+
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat2)*Math.cos(newlat);
var c = 2* Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R *c;
x.innerHTML="distance:"+d;
}
</script>
答案 1 :(得分:0)
javascript Math Object
没有toRad()
方法。
以下代码包含2个函数deg2rad()
和Haversine()
。
<!DOCTYPE html>
<html>
<body>
<p id="demo"> Distance calculations </p>
<button onclick="getLocation()"> Please Work</button>
<script type="text/javascript">
var lat2= 40.084361;
var lng2= -83.100484;
function deg2rad(degrees){
radians = degrees * (Math.PI/180);
return radians;
}
function Haversine(lat1,lon1,lat2,lon2) {
deltaLat = lat2 - lat1 ;
deltaLon = lon2 - lon1 ;
earthRadius = 6371; // 3959 in miles 6371 in meters.
alpha = deltaLat/2;
beta = deltaLon/2;
a = Math.sin(deg2rad(alpha)) * Math.sin(deg2rad(alpha)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(deg2rad(beta)) * Math.sin(deg2rad(beta)) ;
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
distance = earthRadius * c;
return distance.toFixed(2);
}
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
var lat1 = position.coords.latitude;
var lng1 = position.coords.longitude;
var distance = Haversine(lat1,lng1,lat2,lng2);
x.innerHTML="Distance: " + distance + "kms"
}
</script