且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

谷歌地图 - 如何以米为单位获得两点之间的距离?

更新时间:2023-02-02 10:25:48

如果您想使用 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>