如何通过Google Maps Javascript API3获得2点之间的距离?

dan*_*007 2 javascript google-maps-api-3

我正在通过Google Maps API(第3版)开发应用程序.

我有两个google.maps.LatLng名为loc1和的对象loc2.

如何以米为单位计算它们之间的距离?

我尝试使用此问题中的各种技巧 将纬度/经度度量转换为米.但是,我质疑我的实施的可靠性.

我也尝试过实现距离矩阵,但我认为距离矩阵只需要计算2点之间的距离就可以了.

谢谢!

Hei*_*ang 9

听起来很简单,我推荐几何库,

https://developers.google.com/maps/documentation/javascript/reference#spherical

computeDistanceBetween(from:LatLng, to:LatLng, radius?:number)
Run Code Online (Sandbox Code Playgroud)

根据我的经验,结果以米为单位.请记住在标记中添加可选的几何库

<script type="text/javascript" 
src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">
</script>
Run Code Online (Sandbox Code Playgroud)