Draw Rectangles in Area on Google Maps

后端 未结 2 435
傲寒
傲寒 2021-02-06 15:10

I\'m drawing an area on Google Maps using the Geometry API. I want to know if it is possible to draw a repeating element onto an area that is dynamic in size?

For exampl

2条回答
  •  误落风尘
    2021-02-06 15:39

    Although I didn't use canvas, how about this code?

    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);
          }
        }
      }
    }
    

    This code works like this image. enter image description here

    I wrote a page that explains the code. http://googlemaps.googlermania.com/google_maps_api_v3/en/poly_containsLocation.html

提交回复
热议问题