2017-05-05 83 views
1

我已经阅读了谷歌地图API计算距离的一些问题,但仍然不适合我。这里是我的代码:谷歌地图 - computeDistanceBetween无法正常工作

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> 

但我仍然得到错误:未捕获的类型错误:在initMap

无法读取属性未定义 “球形”我做错了什么?我必须启用一些自定义API吗?

回答

6

你在你的src url中有'libraries'参数两次。

删除第二个: &库=地方

...并保持第一位: &库=几何,则以

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

哎哟我白痴...谢谢! – general666

+1

没问题。有时候只需要一双新鲜的眼睛。 –

2

没有与请求的URL的一个问题,参数重复。

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