javascript - Google maps - computeDistanceBetween not working - Stack Overflow

I have read some issues with calculate distance on google maps API but still not working for me. Here i

I have read some issues with calculate distance on google maps API but still not working for me. Here is my code:

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.sphericalputeDistanceBetween(loc1,loc2));
}

This is how I call library:

<script src=",places&key=MY_KEY&libraries=places&callback=initMap" async defer></script>

But I still get error: Uncaught TypeError: Cannot read property 'spherical' of undefined at initMap

What I'm doing wrong? Do I have to enable some custom API?

I have read some issues with calculate distance on google maps API but still not working for me. Here is my code:

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.puteDistanceBetween(loc1,loc2));
}

This is how I call library:

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

But I still get error: Uncaught TypeError: Cannot read property 'spherical' of undefined at initMap

What I'm doing wrong? Do I have to enable some custom API?

Share Improve this question asked May 5, 2017 at 9:01 general666general666 1,0392 gold badges16 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You have the 'libraries' parameter in your src url twice.

Remove the second one: &libraries=places

...and keep the first one: &libraries=geometry,places

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

There is a problem with request url, libraries parameter duplicates.

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

In order to use spherical class you have to include 'geometry' into requesting libraries list - check 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./maps/api/js?key=YOUR_API_KEY&libraries=geometry">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742288993a4415844.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信