【问题标题】:Google Maps merge rectangles into one polygon and search it谷歌地图将矩形合并为一个多边形并搜索它
【发布时间】:2013-07-26 19:40:25
【问题描述】:

下面是我的代码,在地图上包含一组边界和矩形图形,表示每个边界项。

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

【问题讨论】:

  • @Dr.Molle 对于公众来说,这并不是真正的重复,因为问题不同。是的,前一个问题的答案与这个问题有关,但该主题涉及标记。在这里我试图合并边界而不是检查标记是否存在。
  • 您的问题是如何将多个边界合并到 1 个多边形中,这正是您在副本中提出的问题以及那里已经回答的问题
  • @Dr.Molle 不,问题不在于如何将边界合并到 1 个多边形中,尽管检查了该解决方案作为正确答案。在那个特定的项目中,我没有理由合并边界。
  • @Dr.Molle 据我所知,您不能将LatLngBounds 传递给多边形的路径属性。它接受LatLng

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


【解决方案1】:

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

  1. 找出所有矩形的顶点坐标
  2. 对这些顶点进行顺时针或逆时针排序(无论 Google 地图需要哪个)
  3. 将这些顶点作为 LatLng 对象数组按排序顺序提供给 Polygon() 构造函数

很抱歉,我没有时间详细介绍如何找到对角或按角度对它们进行排序,如果这还不够,希望其他人可以帮助您。

【讨论】:

  • 谢谢!我试过了,它创建了单独的多边形,就像单独的矩形一样:(困难的部分是只选择沿着外部矩形的顶点来创建一个封闭的多边形。我以geocodezip.com/v3_polygon_example.html为例,但脚本预设了所有坐标而我必须动态创建它们。
  • 啊,在您的示例中,没有一个矩形重叠。如果有,那么您有第三个问题,您需要找到定义要创建的多边形外部的点。在那种情况下,我会以不同的方式处理这个问题。我将通过一次添加一个矩形并在每一步丢弃现有多边形内的顶点来构建多边形。这假设您有某种方法可以检查一个点是否在多边形中,这里有一些代码:github.com/tparkin/Google-Maps-Point-in-Polygon。我会更新我的答案以反映这一点。
猜你喜欢
  • 2015-08-26
  • 2012-11-24
  • 2017-07-22
  • 1970-01-01
  • 2012-07-03
  • 2016-12-19
  • 2017-04-30
  • 2012-07-01
相关资源
最近更新 更多