Google Maps和RouteBoxer不会显示多边形线

时间:2013-01-31 02:41:07

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

提前感谢您提供的任何帮助!我在谷歌地图API V3中使用RouteBoxer,但由于某种原因我无法显示行。我担心函数根本没有运行,而且我的项目的下一步是必要的:传递lat和long来找到路线上的pois。看到地图上的线条将帮助我确保它正确运行。

这是我的代码

<script>
        var directionsDisplay;
        var directionsService = new google.maps.DirectionsService();
        var map;
        var routeBoxer = null;
        var boxpolys = null;
        var rdistance = 20; // km

        function initialize() {
          //directionspanelstuff
          //directionsdisplaystuff
          //mapoptions
          map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
          directionsDisplay.setMap(map);
          routeBoxer = new RouteBoxer();
        }

        function calcRoute() {
          //startendwaypoints

          directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
              directionsDisplay.setDirections(response);
              var route = response.routes[0];
              var summaryPanel = document.getElementById("directions_panel");

              // Box the overview path of the first route
                var path = result.routes[0].overview_path;
                var boxes = routeBoxer.box(path, rdistance);
                clearBoxes();
                drawBoxes(boxes);

                for (var i = 0; i < boxes.length; i++) {
                  var bounds = box[i];
                  // Perform search over this bounds 
                }
            }
          });
        }

        // Draw the array of boxes as polylines on the map
        function drawBoxes(boxes) {
          boxpolys = new Array(boxes.length);
          for (var i = 0; i < boxes.length; i++) {
            boxpolys[i] = new google.maps.Rectangle({
              bounds: boxes[i],
              fillOpacity: 0,
              strokeOpacity: 1.0,
              strokeColor: '#000000',
              strokeWeight: 1,
              map: map
            });
          }
        }

        // Clear boxes currently on the map
        function clearBoxes() {
          if (boxpolys != null) {
            for (var i = 0; i < boxpolys.length; i++) {
              boxpolys[i].setMap(null);
            }
          }
          boxpolys = null;
        }
    </script>

1 个答案:

答案 0 :(得分:1)

javascript控制台指出了4个javascript错误:

  1. mapOptions未定义(可能不是真正的问题)
  2. directionsDisplay为null(未初始化)
  3. 结果未定义(拼写错误或剪切和粘贴错误)
  4. 框未定义(拼写错误)
  5. working example

    代码段

    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;
    var routeBoxer = null;
    var boxpolys = null;
    var rdistance = 20; // km
    
    function initialize() {
      //directionspanelstuff
      //directionsdisplaystuff
      //mapoptions
      map = new google.maps.Map(document.getElementById("map_canvas"), {
        zoom: 10,
        center: new google.maps.LatLng(41.084951, 29.016048),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
      directionsDisplay = new google.maps.DirectionsRenderer();
      directionsDisplay.setMap(map);
      routeBoxer = new RouteBoxer();
      calcRoute();
    }
    
    function calcRoute() {
      var start = document.getElementById('start').value;
      var end = document.getElementById('end').value;
      var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      };
      //startendwaypoints
    
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
          var route = response.routes[0];
          var summaryPanel = document.getElementById("directions_panel");
    
          // Box the overview path of the first route
          var path = response.routes[0].overview_path;
          var boxes = routeBoxer.box(path, rdistance);
          clearBoxes();
          drawBoxes(boxes);
    
          for (var i = 0; i < boxes.length; i++) {
            var bounds = boxes[i];
            // Perform search over this bounds 
          }
        }
      });
    }
    
    // Draw the array of boxes as polylines on the map
    function drawBoxes(boxes) {
      boxpolys = new Array(boxes.length);
      for (var i = 0; i < boxes.length; i++) {
        boxpolys[i] = new google.maps.Rectangle({
          bounds: boxes[i],
          fillOpacity: 0,
          strokeOpacity: 1.0,
          strokeColor: '#000000',
          strokeWeight: 1,
          map: map
        });
      }
    }
    
    // Clear boxes currently on the map
    function clearBoxes() {
      if (boxpolys != null) {
        for (var i = 0; i < boxpolys.length; i++) {
          boxpolys[i].setMap(null);
        }
      }
      boxpolys = null;
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body,
    #map_canvas {
      margin: 0;
      padding: 0;
      height: 100%;
    }
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/routeboxer/src/RouteBoxer.js"></script>
    <input id="start" type="text" onchange="calcRoute();" value="chicago, il"></input>
    
    <input id="end" type="text" onchange="calcRoute();" value="st louis, mo"></input>
    
    <div id="map_canvas" style="height: 400px; width:500px;"></div>
    <div id="info"></div>