我正在尝试计算KM中两个纬度与经度之间的距离,并在标签或段落中显示该距离。
lat1
和lon1
。我能够弄清楚。lat2
和lon2
。我也能弄清楚。现在,一旦有了lat1
,lon1
,lat2
和lon2
-我需要找到这些坐标之间的距离。以下是我的代码,但是一旦我发出警告,它就会以NaN
的形式给出距离。我不确定这是怎么回事。这也是我在这里做的正确方法吗?
<!DOCTYPE html>
<html>
<style>
body {
margin: 0;
padding: 0;
font: 12px sans-serif;
}
h1, p {
margin: 0;
padding: 0;
}
#map_div{
width:400px;
height:400px;
margin: 0 auto;
}
#outer {
width:400px;
height:50px;
margin:0 auto;
text-align:center;
}
#outer input[type="text"] {
display: block;
margin: 0 auto;
}
</style>
<body>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script>
<div id="outer">
<p id="distance_text">this is 201 km away from</p>
<input type="text" name="location" value="Current Location"><br>
<div id="map_div"></div>
</div>
</body>
<script type="text/javascript">
var lat1;
var lon1;
var lat2;
var lon2;
var geocoder = new google.maps.Geocoder();
var address = "1750 riverside drive, ottawa, ontario, canada";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
lat1 = results[0].geometry.location.lat();
lon1 = results[0].geometry.location.lng();
}
});
var x = document.getElementById("distance_text");
getLocation();
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
lat2 = position.coords.latitude;
lon2 = position.coords.longitude;
}
var distance = getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2);
alert(distance);
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the 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; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
</script>
</html>
计算后,我需要用实际距离替换201 km
。这是我的jsfiddle
答案 0 :(得分:1)
您可以使用Google Maps Distance方法,但需要LatLng
个对象
尝试一下:
const from = new google.maps.LatLng(lat1, lng1);
const to = new google.maps.LatLng(lat2, lng2);
const distance = google.maps.geometry.spherical.computeDistanceBetween(from,to)
这是有效的JSfiddle,您可能必须在地图脚本标签中添加API密钥: https://jsfiddle.net/ruokyanv/
更改p标签距离:是的,您可以在html中注入js文件并使用jquery $:
var distance = `this is ${distance} km away from`
$('#distance_text').val(distance)
让我知道问题是否仍然存在。
答案 1 :(得分:1)
您可以使用 HERE 地图 API。
var distance = "https://route.ls.hereapi.com/routing/7.2/calculateroute.json?apiKey="+ apiKey +"&mode=balanced;car&waypoint0=geo!"+startingPoint+"&waypoint1=geo!"+endingPoint;
var xmlHttp = new XMLHttpRequest();
xmlHttp .open( "GET", distance, false );
xmlHttp .send( null );
var routeResponse = JSON.parse(xmlHttp .responseText);
var distance = (routeResponse.response.route[0].summary.distance);
startingPoint 和endingPoint 基本上是坐标(纬度和经度)。