javascript - 在谷歌地图上绘制矩形区域

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

我正在使用 Geometry API 在 Google map 上绘制一个区域。我想知道是否可以将重复元素绘制到大小动态的区域?

例如,如果我把我的区域画成这样:

enter image description here

然后我希望能够点击“下一步”并看到类似这样的内容,在该区域中绘制了矩形,但前提是它们适合。即,它们必须是“完整”矩形,而不是部分矩形:

enter image description here

唯一的问题是,我不完全确定该怎么做。我会使用 HTML5 <canvas>但不幸的是,这需要尽可能对浏览器友好,但如果它必须是 <canvas>那就这样吧!

最佳答案

虽然我没有用canvas,但是这段代码呢?

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

此代码的工作方式类似于此图像。 enter image description here

我写了一个解释代码的页面。 http://googlemaps.googlermania.com/google_maps_api_v3/en/poly_containsLocation.html

关于javascript - 在谷歌地图上绘制矩形区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13381497/

相关文章:

javascript - 根据reactjs中的条件设置状态

javascript - 多输入,用户只能输入其中之一

php - 如何在实时服务器中执行 json 解码?

jquery - Google Maps API KML 图层更改每个图层的颜色

reactjs - React jest testing Google Maps Api,Uncaught TypeError : this. autocomplete.addListener 不是函数

javascript - Cookie Bar 未在本地显示

php - 如果浏览器是 IE,则更改文件扩展名

javascript - S3 :HeadObject - returns 403 response

javascript - Rails .js.erb 模板不再适用于 Webpack

jquery - Symfony2 如何与 JQuery 配合使用?什么是 Assets 转储?为什么?