两个位置之间的距离 - Google地图

时间:2012-04-18 14:10:49

标签: javascript google-maps-api-3 distance

我正在尝试使用谷歌地图找到两点之间的位置。这是我正在使用的代码:

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

这是我从控制台得到的错误:

  

未捕获的TypeError:无法读取未定义的属性'sphere'

1 个答案:

答案 0 :(得分:51)

如果您还没有这样做,请在<script>标记中明确添加几何库 src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">