Google地图将矩形合并为一个多边形并进行搜索

时间:2013-07-26 19:40:26

标签: javascript google-maps google-maps-api-3 geometry

下面是我的代码,地图上的边界和矩形图形数组代表每个边界项。

http://jsfiddle.net/XffyE/4/

是否可以将多个矩形/边界合并为一个多边形?目标是在通过合并矩形创建的多边形内进行搜索。

例如,搜索合并边界内的位置而不是单独搜索每个边界。

    function createBounds() {

        var bounds = new Array();

        bounds[0] = new google.maps.LatLngBounds(
            new google.maps.LatLng(25.941886953491675, -80.17411103748543),
            new google.maps.LatLng(25.947676224813897, -80.16767330177947)
        );
        bounds[1] = new google.maps.LatLngBounds(
            new google.maps.LatLng(25.941886953491675, -80.16767330177947),
            new google.maps.LatLng(25.94622890698334, -80.1644544339265)
        );
        bounds[2] = new google.maps.LatLngBounds(
            new google.maps.LatLng(25.927413775186118, -80.1644544339265),
            new google.maps.LatLng(25.94622890698334, -80.15962613214703)
        );
        bounds[3] = new google.maps.LatLngBounds(
            new google.maps.LatLng(25.927413775186118, -80.15962613214703),
            new google.maps.LatLng(25.931755728677782, -80.15801669822054)
        );
        bounds[4] = new google.maps.LatLngBounds(
            new google.maps.LatLng(25.927413775186118, -80.15801669822054),
            new google.maps.LatLng(25.933203046508336, -80.15318839644107)
        );
        bounds[5] = new google.maps.LatLngBounds(
            new google.maps.LatLng(25.92886109301667, -80.15318839644107),
            new google.maps.LatLng(25.933203046508336, -80.15157896251458)
        );

        drawRectangles(bounds);   
    }

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

1 个答案:

答案 0 :(得分:1)

有趣的问题,简短的回答是:

  1. 查找所有矩形顶点的坐标
  2. 按顺时针或逆时针方向对这些顶点排序(无论谷歌地图需要哪个)
  3. 将这些顶点作为LatLng对象数组按排序顺序提供给Polygon()构造函数
  4. 很抱歉,我没有时间详细了解如何找到对角或者按角度对它们进行排序,希望其他人可以帮助你,如果这还不够。

相关问题