Google Maps-无效之间的计算限制



我已经阅读了一些在Google Maps 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>

但我仍然会遇到错误:unturew typeError:无法读取未定义的属性"球形" 在initmap

我在做错什么?我必须启用一些自定义API吗?

您在SRC URL中有'库'参数两次。

删除第二个:&amp; libraries = place

...并保留第一个:&amp; libraries =几何,位置

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

请求URL, libraries 参数重复。

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

要使用球形类,您必须将"几何"包括在请求库列表中 - 检查文档:

// 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">

最新更新