【问题标题】:Google Maps check if circle area is visible within map boundaries谷歌地图检查圆圈区域是否在地图边界内可见
【发布时间】:2012-09-03 14:40:13
【问题描述】:

我有一个包含圆形区域的 Google 地图。

我想知道圆圈当前是否在地图边界内可见。

到目前为止我发现的是检查圆圈的中心是否在边界内,但我想检查整个圆圈而不仅仅是它的中心。

我检查地图当前中心是否在圆圈边界内的代码是这样的:

            google.maps.event.addListener(map, 'bounds_changed', function() 
            {                    
                var circleBounds = circle.getBounds();
                console.log(circleBounds.contains(map.getCenter()));
            });

所以我想要的是这样的东西,这当然是不正确的:

circleBounds.contains(map.getBounds());

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:
    1. 确定圆的最北端、最南端、最西端和最东端 LATLNG 是什么。你可以找到这个给定圆半径

    2. 确定是否所有点都在视口范围内

    3. 如果为真,那么是的,圆圈必须是可见的!

    你真的应该回到你的旧问题并接受它们(点击最佳答案旁边的复选标记大纲)。这就是您对回答者提供的辛勤工作表示感谢的方式。

    【讨论】:

    • 他说他想检查地图是否在圆圈内,而不是圆圈是否在地图内。对我来说没有多大意义,但我知道什么? :)
    【解决方案2】:

    这是一个古老的问题 - 互联网时代的恐龙 - 但如果您要比较两个界限,那么以下情况成立 a fortiorin'est-ce pas em>?:

    if(a.getBounds().contains(b.getBounds().getNorthEast()) 
    && a.getBounds().contains(b.getBounds().getSouthWest()))
    {console.log('B is within A... Bounds are RECTANGLES: \
                  You only need to test two \ 
                  diagonally-opposing corners')};
    

    这是因为对于矩形 R,SW 是 (max(x),min(y)); NE 是 (min(x),max(y)) - 因此 R 中的所有 (x,y) 都包含在测试范围内。

    我当然希望是这样 - 这就是我进行所有边界比较的方式...

    【讨论】:

      【解决方案3】:

      感谢蒂娜 CG Hoehr。

      以防万一其他人想要这个,这是我的问题的代码:

      // Get the bounds
      var circleBounds = circle.getBounds();      
      var ne = circleBounds.getNorthEast(); // LatLng of the north-east corner
      var sw = circleBounds.getSouthWest();
      var nw = new google.maps.LatLng(ne.lat(), sw.lng());
      var se = new google.maps.LatLng(sw.lat(), ne.lng());
      
      google.maps.event.addListener(map, 'bounds_changed', function() 
      {                    
          var mapBounds = map.getBounds();
      
          // Log whether the circle is inside or outside of the map bounds
          if(mapBounds.contains(ne))
          {
              console.log("northeast is viewable");
          }
          if(mapBounds.contains(sw))
          {
              console.log("southwest is viewable");
          }
          if(mapBounds.contains(nw))
          {
              console.log("northwest is viewable");
          }
          if(mapBounds.contains(se))
          {
              console.log("southeast is viewable");
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-24
        • 2019-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-08
        • 1970-01-01
        相关资源
        最近更新 更多