Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使矩形在另一个矩形绘制之前可见_Javascript_Google Maps - Fatal编程技术网

Javascript 如何使矩形在另一个矩形绘制之前可见

Javascript 如何使矩形在另一个矩形绘制之前可见,javascript,google-maps,Javascript,Google Maps,我想在谷歌地图上画一个矩形。成功绘制矩形后,我得到了绑定值。但矩形不可见,这是我的问题。请帮我画一个可见的矩形,直到另一个矩形画出来 PolygonComplete(多边形)上的函数{ function onPolygonComplete(polygon) { var bounds, paths, sw, ne, ystep, xstep, boxH, boxW, posArry, flag, pos, x, y, i, box, maxBoxCnt;

我想在谷歌地图上画一个矩形。成功绘制矩形后,我得到了绑定值。但矩形不可见,这是我的问题。请帮我画一个可见的矩形,直到另一个矩形画出来

PolygonComplete(多边形)上的函数{
      function onPolygonComplete(polygon) {
     var bounds, paths, sw, ne, ystep, xstep,
     boxH, boxW, posArry, flag, pos,
     x, y, i, box, maxBoxCnt;

   //Delete old boxes.
   boxes.forEach(function(box, i) {
    box.setMap(null);
        delete box;
      });

     //Calculate the bounds that contains entire polygon.
       bounds = new google.maps.LatLngBounds();
      paths = polygon.getPath();
     paths.forEach(function(latlng, i){
       bounds.extend(latlng);
       });


      //Calculate the small box size.
       maxBoxCnt = 8; 
     sw = bounds.getSouthWest();
      ne = bounds.getNorthEast();
       ystep = Math.abs(sw.lat() - ne.lat()) / maxBoxCnt;
        boxH = Math.abs(sw.lat() - ne.lat()) / (maxBoxCnt + 1);
       xstep = Math.abs(sw.lng() - ne.lng()) / maxBoxCnt;
      boxW = Math.abs(sw.lng() - ne.lng()) / (maxBoxCnt + 1); 

        for (y = 0; y < maxBoxCnt; y++) {
          for (x = 0; x < maxBoxCnt; x++) {
           //Detect that polygon is able to contain a small box.
        bounds = new google.maps.LatLngBounds();
          posArry = [];
         posArry.push(new google.maps.LatLng(sw.lat() + ystep * y, sw.lng() + xstep *     x));
  posArry.push(new google.maps.LatLng(sw.lat() + ystep * y, sw.lng() + xstep * x + boxW));
  posArry.push(new google.maps.LatLng(sw.lat() + ystep * y + boxH, sw.lng() + xstep * x));
  posArry.push(new google.maps.LatLng(sw.lat() + ystep * y + boxH, sw.lng() + xstep * x + boxW));
  flag = true;
  for (i = 0; i < posArry.length; i++) {
    pos = posArry[i];
    if (flag) {
      flag = google.maps.geometry.poly.containsLocation(pos, polygon);
      bounds.extend(pos);
    }
  }

  //Draw a small box.
  if (flag) {
    box = new google.maps.Rectangle({
      bounds : bounds,
      map : mapCanvas,
      strokeColor: '#00ffff',
      strokeOpacity: 0.5,
      strokeWeight: 1,
      fillColor: '#00ffff',
      fillOpacity : 0.5,
      clickable: false
    });
    boxes.push(box);
  }
}
变量边界、路径、sw、ne、ystep、xstep、, boxH、boxW、posArry、flag、pos、, x、 y,i,box,maxBoxCnt; //删除旧框。 框。forEach(函数(框,i){ box.setMap(空); 删除框; }); //计算包含整个多边形的边界。 bounds=新的google.maps.LatLngBounds(); path=polygon.getPath(); 路径.forEach(函数(latlng,i){ 边界扩展(latlng); }); //计算小盒子的大小。 maxBoxCnt=8; sw=bounds.getSouthWest(); ne=bounds.getNorthEast(); ystep=Math.abs(sw.lat()-ne.lat())/maxBoxCnt; boxH=Math.abs(sw.lat()-ne.lat())/(maxBoxCnt+1); xstep=Math.abs(sw.lng()-ne.lng())/maxBoxCnt; boxW=Math.abs(sw.lng()-ne.lng())/(maxBoxCnt+1); 对于(y=0;y } }


也请参考此演示:

在矩形的“单击”事件侦听器中,通过设置setMap(null),然后将其边界设置为最小值来删除矩形。可能您应该删除这些行,并在“mousemove”事件上检查“state”变量,而不是使用getmap

if (state==1) {
    rect.setBounds(toBounds(pt1, event.latLng));
}

请参阅更新的

请参考此链接以获取代码尝试此。。我想这会给你一个想法…谢谢你的建议,但我需要更正我的代码本身。这只是我现在的问题