javascript 两个地点之间的距离 - 谷歌地图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10211187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:02:52  来源:igfitidea点击:

Distance between two locations - Google Maps

javascriptgoogle-maps-api-3distance

提问by cclerv

I'm trying to find the location between two points using google maps. Here is the code that I'm working with:

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

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

This is the error I get from the console:

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

Uncaught TypeError: Cannot read property 'spherical' of undefined

未捕获的类型错误:无法读取未定义的属性“球形”

回答by Heitor Chang

If you haven't done so, explicitly add the geometry library in your <script>tag src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">

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