2017-05-05 4 views
1

私はgoogle maps APIで距離を計算することでいくつかの問題を読んだが、まだ私のために働いていない。ここに私のコードは次のとおりです。Googleマップ - computeDistanceBetween not working

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

しかし、私はまだエラーを取得:

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)); 
} 

これは、私は、ライブラリを呼び出す方法です捕捉されない例外TypeErrorを:プロパティを読み取ることができません未定義 の '球状' initMap

で私は間違っているの?カスタムAPIを有効にする必要がありますか?

答えて

6

あなたのsrc urlに 'libraries'パラメータが2回あります。

秒1を削除します。 &ライブラリ=場所

を...と、最初のものを保つ: &ライブラリ=幾何学、場所

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

痛い私は馬鹿だ...ありがとう:documentationをチェック - 球形のクラスを使用すると、ライブラリのリストを要求に「ジオメトリ」を含めるようにしている使用するためには

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

! – general666

+1

問題ありません。ときには新鮮な目のペアが必要な場合もあります。 –

2

リクエストURL、に問題がありますライブラリパラメータの重複。

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