谷歌地图 - 如何以米为单位获得两点之间的距离? [英] Google Maps - How to get the distance between two point in metre?
本文介绍了谷歌地图 - 如何以米为单位获得两点之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这些坐标:
(45.463688, 9.18814)
(46.0438317, 9.75936230000002)
我需要(我认为是 Google API V3)以米为单位获得这 2 个点之间的距离.我该怎么做?
and I need (trought Google API V3, I think) to get the distance between those 2 points in metre. How can I do it?
推荐答案
如果您想使用 v3 谷歌地图 API,这里有一个可以使用的函数:注意:您必须将 &libraries=geometry
添加到您的脚本源
If you're looking to use the v3 google maps API, here is a function to use:
Note: you must add &libraries=geometry
to your script source
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>
<script>
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);
alert(calcDistance(p1, p2));
//calculates distance between two points in km's
function calcDistance(p1, p2) {
return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}
</script>
这篇关于谷歌地图 - 如何以米为单位获得两点之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文