我在这里浏览过,虽然没有找到修复程序,但下面的代码似乎可以正常工作。我正在尝试从两个纬度和经度中获取与Google地图一起使用的距离。我可以使用所有标准方法在另一台服务器上工作,例如google api等。 我无法计算出“ dist”。在我使用的服务器上,XML存在问题。我花了几个小时在此上,并且猜想这是一个简单的解决方法(脑雾在起作用)
测试html
FROM<br>
<input id="lat1" type="text" value="">Latitude<br>
<input id="lon1" type="text" value="">Longitude<br>
TO<br>
<input id="lat2" type="text" value="">Latitude<br>
<input id="lon2" type="text" value="">Longitude<br>
<hr>
<input id="dist" type="text" value="">Distance<br>
<br>
<button id="calc">Calculate</button>
<br>
<div id="dist_text"></div>
<div id="lat1_text"></div>
<div id="lon1_text"></div>
<div id="lat2_text"></div>
<div id="lon2_text"></div>
<div id="radtheta_text"></div>
脚本
$('#calc').click(function()
{
var lat1=$('#lat1').val();
var lon1=$('#lon1').val();
var lat2=$('#lat2').val();
var lon2=$('#lon2').val();
var dist='0';
var unit = 'M';
if ((lat1 == lat2) && (lon1 == lon2)) {
$('#dist').val('ERROR');
}
else {
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
// Edited to remove this below, incase anyone else finds it where i did.
//var dist = lon1-lon2;
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
//var dist2 = dist.toFixed(4);
//return dist;
$('#dist').val(dist.toFixed(4));
$('#dist_text').text('Distance: ' + dist);
$('#lat1_text').text('Latitude 1: ' + lat1);
$('#lon1_text').text('Longitude 1: ' + lon1);
$('#lat2_text').text('Latitude 2: ' + lat1);
$('#lon2_text').text('Longitude 2: ' + lon2);
$('#radtheta_text').text('radtheta: ' + radtheta);
}
});