Javascript 哈弗森公式不起作用/显示的距离?
我有一个我声明的预定位置。我正在从地理定位中获取第二个位置。然后我试着用标准公式计算距离。我无法让它输出距离。。。我在做什么?对不起,这里没有程序员Javascript 哈弗森公式不起作用/显示的距离?,javascript,html,geolocation,Javascript,Html,Geolocation,我有一个我声明的预定位置。我正在从地理定位中获取第二个位置。然后我试着用标准公式计算距离。我无法让它输出距离。。。我在做什么?对不起,这里没有程序员 <!DOCTYPE html> <html> <body> <p id="demo"> Distance calculations </p> <button onclick="getLocation()"> Please Work</button> <scri
<!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>
距离计算
请工作
var x=document.getElementById(“演示”);
var lat2=40.084361;
var lng2=-83.100484;
函数getLocation()
{
if(导航器.地理位置)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML=“不支持地理位置”;}
}
功能显示位置(位置)
{
x、 innerHTML=“Latitide:”+position.coords.latitude+
经度:“+position.coords.Longitude;”
C
var newlat=位置坐标纬度;
var newlng=位置坐标经度;
var R=6371;公里
var dLat=(newlat-lat2.toRad();
var dLon=(newlng-lng2).toRad();
var lat2=lat2.toRad();
var newlat=newlat.toRad();
变量a=Math.sin(dLat/2)*Math.sin(dLat/2)+
数学sin(dLon/2)*数学sin(dLon/2)*数学cos(lat2)*数学cos(newlat);
var c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
var d=R*c;
x、 innerHTML=“距离:+d);
您有几个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>
距离计算
请工作
var x=document.getElementById(“演示”);
var lat2=40.084361;
var lng2=-83.100484;
函数getLocation()
{
if(导航器.地理位置)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML=“不支持地理位置”;}
}
功能显示位置(位置)
{
x、 innerHTML=“Latitide:”+position.coords.latitude+
“
经度:”+position.coords.Longitude;
var newlat=位置坐标纬度;
var newlng=位置坐标经度;
var R=6371;//km
var dLat=(newlat-lat2.toRad();
var dLon=(newlng-lng2).toRad();
var lat2=lat2.toRad();
var newlat=newlat.toRad();
变量a=Math.sin(dLat/2)*Math.sin(dLat/2)+
数学sin(dLon/2)*数学sin(dLon/2)*数学cos(lat2)*数学cos(newlat);
var c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
var d=R*c;
x、 innerHTML=“距离:”+d;
}
javascript数学对象
没有toRad()
方法。
以下代码有两个函数deg2rad()
和Haversine()
距离计算
请工作
var lat2=40.084361;
var lng2=-83.100484;
功能deg2rad(度){
弧度=度*(数学PI/180);
返回弧度;
}
功能Haversine(lat1、lon1、lat2、lon2){
deltaLat=lat2-lat1;
deltaLon=lon2-lon1;
地球半径=6371;//3959英里6371米。
α=deltaLat/2;
β=deltaLon/2;
a=数学sin(deg2rad(alpha))*数学sin(deg2rad(alpha))+数学cos(deg2rad(lat1))*数学cos(deg2rad(lat2))*数学sin(deg2rad(beta))*数学sin(deg2rad(beta));
c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
距离=接地半径*c;
返回距离。toFixed(2);
}
var x=document.getElementById(“演示”);
函数getLocation()
{
if(导航器.地理位置)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML=“此浏览器不支持地理位置。”;}
}
功能显示位置(位置)
{
var lat1=位置坐标纬度;
var lng1=位置坐标经度;
var距离=哈弗斯线(lat1、lng1、lat2、lng2);
x、 innerHTML=“距离:”+距离+“公里”
}
我确实试过了,但我看不出有什么不起作用,但我单击按钮,什么也没发生,而不是之前至少显示了位置。对不起,这是整个页面。脚本后面只有一个and标记。很多人从Chris Venese的网站movable-type.co.uk复制了他在JS中的Haversine()实现。他对toRad()方法的使用是他编写的一个扩展,它需要包含包含他的Geostatic类定义的代码示例:
<!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