我正在构建应用程序,我使用以下代码获取用户的latitude
和longitude
<!DOCTYPE html> <html> <body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script> 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) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude; } </script>
</body> </html>
现在我想将它转换为公里,以便我可以与其他纬度和经度进行比较,并以公里计算它们之间的差异。
答案 0 :(得分:0)
考虑到纬度和经度是一个特定的点,你不能直接将它转换为千米,这是一个距离,即分隔两个点的长度。
但是你可以用数学公式得到两个坐标(lat + long)之间的距离。 我不是很擅长数学,但你可以在Google上通过简单的搜索找到这样的公式: here is the first result
您可能还会在此主题上找到有用的内容: How to convert latitude or longitude to meters?
答案 1 :(得分:0)
这个脚本对你有用,但它在php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
使用的功能
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>";
答案 2 :(得分:0)
您正在寻找的是Haversine formula;有一个PHP实现here。