0

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

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

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

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

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

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

circleBounds.contains(map.getBounds());
4

3 回答 3

1
  1. Determine what the northmost, southmost, westmost, and eastmost LATLNGs of the circle are. You can find this given the circle radius

  2. Determine if all points are inside the viewport bounds

  3. If true, then yes, the circle must be viewable!

and you really should go back to your old questions and ACCEPT them (click on the check mark outline next to the best answer). This is how you show your appreciation for the hard work your answerers provided.

于 2012-09-03T15:10:34.703 回答
1

这是一个古老的问题——互联网时代的恐龙时代——但如果你要比较两个界限,那么下面的问题更是如此n'est-ce pas ?:

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)) - 因此x,yR 中的所有 ( ) 都包含在测试中。

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

于 2016-07-25T06:39:40.210 回答
0

谢谢蒂娜 CG 霍尔。

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

// 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");
    }
});
于 2012-09-04T07:15:58.360 回答