无法使用谷歌地图 API v3 计算道路距离

Unable to calculate road distance using Google Maps API v3

本文关键字:v3 计算 道路 距离 API 谷歌地图      更新时间:2023-09-26

我在运行以下代码时收到两个错误:1. 未捕获的无效值错误:设置位置的参数无效:[对象对象]2. 未捕获类型错误:无法调用未定义的方法"包含"

编辑:我可以使用相同的标记标签来创建多个标记,就像我在下面所做的那样吗?

基本上是尝试将标记添加到一组经度并计算到公共点的距离:

<script type="text/javascript">
        var map;
        var mapOptions;
        var lat = [33, 34, 35, 36];
        var lon = [-84, -86, -89, -100];
        var src = [30, -90];
        var dist = [];
        var marker;
        var directionsService = new google.maps.DirectionsService();
        var directionsRequest;
        var directionsRenderer;
        var markerPoints =  [{"location": new google.maps.LatLng(lat[0], lon[0])}, 
                             {"location": new google.maps.LatLng(lat[1], lon[1])},
                             {"location": new google.maps.LatLng(lat[2], lon[2])},
                             {"location": new google.maps.LatLng(lat[3], lon[3])}];
          function initialize() {
            directionsRenderer = new google.maps.DirectionsRenderer();
            mapOptions = {
              center: new google.maps.LatLng(33.75, -84.39),
              mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("map-canvas"),
                    mapOptions);
            directionsRenderer.setMap(map);
            marker = new google.maps.Marker({
                 position: new google.maps.LatLng(src[0], src[1]),
                 //icon: 'http://maps.google.com/mapfiles/marker.png',
                 map: map
              });
            for(var iter = 0; iter < lat.length; iter++)
            {
                placeMarker(iter);
                getDistances(iter);
            }
          }
          function placeMarker(iter)
          {
                 marker = new google.maps.Marker({
                 position: markerPoints[iter],
                 //icon: 'http://maps.google.com/mapfiles/marker_green.png',
                 map: map
              });
          }
          function getDistances(iter)
          {
              directionsRequest = {
                    origin: new google.maps.LatLng(src[0], src[1]),
                    destination: markerPoints[iter],
                    travelMode: google.maps.TravelMode.DRIVING,
              };
              directionsService.route(directionsRequest, function(response, status){
                  if(status == google.maps.DirectionsStatus.OK)
                      {
                            dist.push(response.routes[0].legs[0].distance);
                      }
                  else
                      {
                        alert("Impossible");
                      }
              });
          }
        google.maps.event.addDomListener(window, 'load', initialize);
        </script>

来了...

问题 #1:未捕获的无效值错误:setPosition 的参数无效:[对象对象]

您的标记点数组存在问题。而不是

var markerPoints =  [{"location": new google.maps.LatLng(lat[0], lon[0])}, 
                     {"location": new google.maps.LatLng(lat[1], lon[1])},
                     {"location": new google.maps.LatLng(lat[2], lon[2])},
                     {"location": new google.maps.LatLng(lat[3], lon[3])}];

它应该是这样的..

 var markerPoints = [new google.maps.LatLng(lat[0], lon[0]),
                     new google.maps.LatLng(lat[1], lon[1]),
                     new google.maps.LatLng(lat[2], lon[2]),
                     new google.maps.LatLng(lat[3], lon[3])];

问题 #2:未捕获的类型错误:无法调用未定义的方法"包含":

如果您将实验性 API 版本(v=3.exp)用于地图 api,那么您会遇到此问题。您还应该使用任何发布版本。您可以通过访问谷歌地图API版本信息部分找到有关不同版本的详细信息

希望这对您有所帮助。