2012-04-18 46 views
22

Googleマップを使用して2つの地点の間の場所を探しています。ここで私が働いているコードは次のとおりです。2つの地点間の距離 - Google Maps

function initialize() { 
      var myOptions = { 
      center: new google.maps.LatLng(36.8813329,-103.6975488), 
      zoom: 4, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions); 

      var impactCoordinates = [ 
       new google.maps.LatLng(37.772323, -122.214897), 
       new google.maps.LatLng(34.1633766,-81.6487862), 
          ]; 
      var ImpactPath = new google.maps.Polyline({ 
       path: impactCoordinates, 
       strokeColor: "#FF0000", 
       strokeOpacity: 1.0, 
       strokeWeight: 2 
      }); 

      ImpactPath.setMap(map); 

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

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

これは私がコンソールから取得エラーです:

Uncaught TypeError: Cannot read property 'spherical' of undefined

答えて

48

まだ行っていない場合は、明示的にジオメトリライブラリを追加します<script>タグ src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">

+2

ありがとう!これはまさに私が欠けていたものです。 – cclerville

+0

上記のURLを使用する必要もあります。つまり、http://maps.google.com/maps/api/js?sensor = false&?libraries = geometry'は機能しません。 – Arthur

+0

@Arthurは疑問符を削除するのを忘れたからです。 '&?libraries = geometry'は'&libraries = geometry'です。 – jimmystormig

関連する問題