Google地图 - computeDistanceBetween无效

时间:2017-05-05 09:01:20

标签: javascript api maps distance

我已经阅读了谷歌地图API计算距离的一些问题,但仍然无法为我工作。这是我的代码:

function initMap() {
   var loc1 = new google.maps.LatLng(37.772323, -122.214897);
   var loc2 = new google.maps.LatLng(34.1633766,-81.6487862);

   console.log(google.maps.geometry.spherical.computeDistanceBetween(loc1,loc2));
}

这就是我调用库的方式:

<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&key=MY_KEY&libraries=places&callback=initMap" async defer></script>

但我仍然得到错误:Uncaught TypeError:无法读取未定义的属性'sphere'     在initMap

我做错了什么?我是否必须启用一些自定义API?

1 个答案:

答案 0 :(得分:2)

请求网址存在问题,图书馆参数重复。

<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&key=MY_KEY&callback=initMap" async defer></script>

要使用球形类,您必须在请求库列表中包含“几何” - 检查documentation

// This example requires the Geometry library. Include the libraries=geometry
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry">