【问题标题】:Google Maps Waypoints more than 8 issueGoogle Maps Waypoints 超过 8 个问题
【发布时间】:2014-12-24 20:37:21
【问题描述】:

网上有很多示例/代码可用于使用多个航点创建 Google 地图。我通过排除所有标记、单击按钮等创建了类似版本的代码。

我正在使用谷歌地图 V3 航点在多个目的地之间创建路线。由于我们不能使用超过 8 个航点,我正在使用批量处理多个航点。在下面的代码中有 19 个 gps 位置,其中 10 个 gps 位置一批处理,另一批处理 9 个。 drawRouteMap 函数被调用来为 10 个(或更少)gps 位置的集合绘制路线。

问题是谷歌地图在每个函数调用中都被覆盖。谷歌地图的输出显示了最新的处理值。任何人都可以建议我哪里出错了

<!DOCTYPE html>
<html>
  <head>
      <title></title>
</head>
<body>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">


var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function calcRoute() {

  map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);
  var msg ="41.077354,-81.511337:41.080647,-81.516579:41.077435,-81.521561:41.075253,-81.521492:41.074604,-81.520309:41.07415,-81.516335:41.073158,-81.514931:41.070534,-81.516563:41.066677,-81.516502:41.063942,-81.516502:41.06514,-81.513458:41.067383,-81.513412:41.069546,-81.513397:41.070778,-81.513382:41.072514,-81.512619:41.071106,-81.507614:41.073326,-81.506195";
  var input_msg=msg.split(":");
  var locations = new Array();      

  for (var i = 0; i < input_msg.length; i++) {
    var tmp_lat_lng =input_msg[i].split(",");
    locations.push(new google.maps.LatLng(tmp_lat_lng[0], tmp_lat_lng[1])); 
  }

  directionsDisplay = new google.maps.DirectionsRenderer();

  var mapOptions = {
  center: locations[0],
  zoom: 12,
  mapTypeId: google.maps.MapTypeId.ROADMAP  
  }
  directionsDisplay.setMap(map);

  var i =locations.length;
  var index =0;

  while(i !=0 ){

        if(i<3){
          var tmp_locations =new Array();
          for (var j=index;j<locations.length;j++) {
            tmp_locations.push(locations[index]);
          }
        drawRouteMap(tmp_locations); 
        i=0; 
        index=locations.length;  
       }

        if( i>= 3 && i<=10) {
           alert("before :fun < 10: i value "+i+" index value"+index);
           var tmp_locations =new Array();
           for (var j=index;j<locations.length;j++) {
             tmp_locations.push(locations[j]);
           }
        drawRouteMap(tmp_locations);
        i=0;
        index=locations.length;
        alert("after fun < 10: i value "+i+" index value"+index);
        }

        if(i > 10) {
        alert("before :fun > 10: i value "+i+" index value"+index);
        var tmp_locations =new Array();
        for (var j=index;j<index+10;j++) {
         tmp_locations.push(locations[j]);
        }
        drawRouteMap(tmp_locations);
        i=i-10; 
        index =index+10;
        alert("after fun > 10: i value "+i+" index value"+index);
        }
   }
}


 function drawRouteMap(locations){

  var start,end;
  var waypts = [];

  for(var k =0;k<locations.length;k++){
  if (k>=1 && k<=locations.length-2) {
      waypts.push({
          location:locations[k],
          stopover:true});
  }
  if(k==0) 
    start=locations[k];

  if(k==locations.length-1) 
     end=locations[k];

 }
   var request = {
      origin: start,
      destination: end,
      waypoints: waypts,
      optimizeWaypoints: true,
      travelMode: google.maps.TravelMode.DRIVING
  };
      console.log(request);
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
            console.log(status);
      directionsDisplay.setDirections(response);
    }
  });


 }

google.maps.event.addDomListener(window, 'load', calcRoute);
    </script>
    <div id="dvMap" style="width: 500px; height: 500px">
    </div>
  </body>
</html>

【问题讨论】:

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


    【解决方案1】:

    您需要为要在地图上显示的每条路线创建一个单独的 DirectionsRenderer 实例。

    var directionsDisplay = [];
    var directionsService = [];
    var map = null;
    var bounds = null;
    
     function drawRouteMap(locations){
    
      var start,end;
      var waypts = [];
    
      for(var k =0;k<locations.length;k++){
      if (k>=1 && k<=locations.length-2) {
          waypts.push({
              location:locations[k],
              stopover:true});
      }
      if(k==0) 
        start=locations[k];
    
      if(k==locations.length-1) 
         end=locations[k];
    
     }
       var request = {
          origin: start,
          destination: end,
          waypoints: waypts,
          optimizeWaypoints: true,
          travelMode: google.maps.TravelMode.DRIVING
      };
          console.log(request);
    
      directionsService.push(new google.maps.DirectionsService());
      var instance = directionsService.length-1;
         directionsDisplay.push(new google.maps.DirectionsRenderer({preservViewport:true}));
      directionsDisplay[instance].setMap(map);
      directionsService[instance].route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          console.log(status);
          if (!bounds) bounds = response.bounds;
          else bounds.union(response.bounds);
          directionsDisplay[instance].setDirections(response);
          if (instance > 0) map.fitBounds(bounds);
        }
      });
     }
    

    working fiddle (but you probably want to connect the two routes)

    working fiddle with common point

    工作代码sn-p:

    var directionsDisplay = [];
    var directionsService = [];
    var map = null;
    
    function calcRoute() {
      var msg = "41.077354,-81.511337:41.080647,-81.516579:41.077435,-81.521561:41.075253,-81.521492:41.074604,-81.520309:41.07415,-81.516335:41.073158,-81.514931:41.070534,-81.516563:41.066677,-81.516502:41.063942,-81.516502:41.06514,-81.513458:41.067383,-81.513412:41.069546,-81.513397:41.070778,-81.513382:41.072514,-81.512619:41.071106,-81.507614:41.073326,-81.506195";
      var input_msg = msg.split(":");
      var locations = new Array();
    
        var bounds = new google.maps.LatLngBounds();
        for (var i = 0; i < input_msg.length; i++) {
            var tmp_lat_lng = input_msg[i].split(",");
            locations.push(new google.maps.LatLng(tmp_lat_lng[0], tmp_lat_lng[1]));
            bounds.extend(locations[locations.length-1]);
        }
    
        var mapOptions = {
            // center: locations[0],
            zoom: 12,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);
        map.fitBounds(bounds);
        google.maps.event.addDomListener(window,'resize',function() {
          google.maps.event.trigger(map,'resize');
          map.fitBounds(bounds);
        });
    
      var i = locations.length;
      var index = 0;
    
      while (i != 0) {
    
        if (i < 3) {
          var tmp_locations = new Array();
          for (var j = index; j < locations.length; j++) {
            tmp_locations.push(locations[index]);
          }
          drawRouteMap(tmp_locations);
          i = 0;
          index = locations.length;
        }
    
        if (i >= 3 && i <= 10) {
          console.log("before :fun < 10: i value " + i + " index value" + index);
          var tmp_locations = new Array();
          for (var j = index; j < locations.length; j++) {
            tmp_locations.push(locations[j]);
          }
          drawRouteMap(tmp_locations);
          i = 0;
          index = locations.length;
          console.log("after fun < 10: i value " + i + " index value" + index);
        }
    
        if (i >= 10) {
          console.log("before :fun > 10: i value " + i + " index value" + index);
          var tmp_locations = new Array();
          for (var j = index; j < index + 10; j++) {
            tmp_locations.push(locations[j]);
          }
          drawRouteMap(tmp_locations);
          i = i - 9;
          index = index + 9;
          console.log("after fun > 10: i value " + i + " index value" + index);
        }
      }
    }
    
    
    function drawRouteMap(locations) {
    
      var start, end;
      var waypts = [];
    
      for (var k = 0; k < locations.length; k++) {
        if (k >= 1 && k <= locations.length - 2) {
          waypts.push({
            location: locations[k],
            stopover: true
          });
        }
        if (k == 0) start = locations[k];
    
        if (k == locations.length - 1) end = locations[k];
    
      }
      var request = {
        origin: start,
        destination: end,
        waypoints: waypts,
        travelMode: google.maps.TravelMode.DRIVING
      };
      console.log(request);
    
      directionsService.push(new google.maps.DirectionsService());
      var instance = directionsService.length - 1;
      directionsDisplay.push(new google.maps.DirectionsRenderer({
        preserveViewport: true
      }));
      directionsDisplay[instance].setMap(map);
      directionsService[instance].route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          console.log(status);
          directionsDisplay[instance].setDirections(response);
        }
      });
    }
    
    google.maps.event.addDomListener(window, 'load', calcRoute);
    html,
    body,
    #dvMap {
      height: 100%;
      width: 100%
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="dvMap"></div>

    【讨论】:

    • geocodezip :我可以在控制台中看到类似“Uncaught TypeError: Cannot read property 'getSouthWest' of undefined”之类的错误。我可以忽略它吗?
    • 现在已修复。适合组合路线的缩放已损坏。
    • 如何修复缩放?我尝试使用 setzoom level ,将缩放、宽度初始化为 100%。但它们都不起作用。
    • 在发布我的最后一条评论之前,我用修复更新了我的答案。 jsfiddle(连接整条路线的那个)和代码 sn-p 现在按预期工作(至少对我来说)。
    • jsfiddle(有共同点)在缩放或拖动时出现问题,而 jsfiddle(连接两个点)是第一个的早期版本
    猜你喜欢
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多