2013-07-26 54 views
1

下面是我的代码,其中包含表示边界项的地图上的边界和矩形图的数组。谷歌地图将矩形合并为一个多边形并搜索它

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

[Google Maps API检查标记是否存在于多个边界中]的可能重复(http://stackoverflow.com/questions/17253929/google-maps-api-check-if-marker-exists-in-multiple-bounds ) –

+0

@ Dr.Molle对于公众来说,它并不是真的重复,因为问题是不同的。是的,前一个问题的答案与这个问题有关,但主题涉及标记。这里我试图合并边界而不检查标记是否存在。 – CyberJunkie

+0

这里你的问题是如何将多个边界合并为1个多边形,而这正是你在副本中询问的内容以及已经在那里回答的问题 –

回答

1

趣味的问题,简单的答案是:

  1. 找到所有的矩形的顶点
  2. 排序的坐标,这些顶点顺时针或逆时针(无论谷歌地图想)
  3. 按排序顺序将这些顶点作为LatLng对象数组输入到Polygon()构造函数

对不起,我没有时间详细了解如何找到相反的角落或按角度排序,希望别人可以帮助你,如果这还不够。

+0

谢谢!我尝试过,它会创建与单独的矩形完全相同的单独多边形:(困难的部分是仅沿外部矩形选取顶点以创建一个封闭多边形。我正在查看此http://www.geocodezip.com/v3_polygon_example。 html的例子,但脚本预设所有的坐标,而我必须动态创建它们 – CyberJunkie

+0

啊,在你的例子中没有任何矩形重叠如果他们有你有第三个问题,你需要找到定义在这种情况下,我会以不同的方式来处理这个问题,我会一次添加一个矩形来构建多边形,并在每个步骤中抛出现有多边形内的顶点,假设您有某种方法检查一个点是否在多边形中,这里的一些代码在这里:https://github.com/tparkin/Google-Maps-Point-in-Polygon。我会更新我的答案来反映这一点。 – nwellcome