0

あなたが提供できる助けを前もって感謝します!Google Maps API V3 で RouteBoxer を使用しているのですが、なぜか線が表示されません。関数がまったく実行されていないのではないかと心配しています。プロジェクトの次のステップである緯度と経度を渡して、ルートに沿ってポアを見つける必要があります。地図上の線を見ると、正しく実行されていることを確認するのに役立ちます.

これが私のコードです

<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>
4

1 に答える 1

1

JavaScript コンソールで指摘された 4 つの JavaScript エラーがあります。

  1. mapOptions が定義されていません (おそらく実際の問題ではありません)
  2. directionDisplay は null (初期化されていません)
  3. 結果が定義されていません (タイプミス、またはカット アンド ペースト エラー)
  4. ボックスが定義されていません (タイプミス)

実施例

コードスニペット:

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>

于 2013-01-31T04:06:29.783 に答える