2012-09-03 80 views
0

我有一個包含圓圈的谷歌地圖。Google地圖檢查地圖邊界是否存在圓圈邊界

我想知道該圓圈當前是否在用戶正在查看的位置邊界內可見。

到目前爲止,我發現的是檢查地圖的中心是否在範圍內,但我想檢查整個地圖,而不僅僅是它的中心。

我的代碼來檢查,如果當前地圖中心是圓的範圍:

  google.maps.event.addListener(map, 'bounds_changed', function() 
      {      
       var circleBounds = circle.getBounds(); 
       // Log into the console whether the center is inside or outside 
       console.log(circleBounds.contains(map.getCenter())); 
      }); 

所以,我想是這樣的,這當然是不正確的:

circleBounds.contains(map.getBounds()) 

回答

1
  1. 確定圓的最北端,最南端,最西端和最東端的LATLNG。你可以找到這個給定圓半徑

  2. 確定是否所有的點都視邊界內

  3. 如果爲真,那麼,圓必須查看!

您真的應該回到您的舊問題並接受他們(點擊最佳答案旁邊的複選標記輪廓)。這就是你如何表達你對答覆者提供的辛勤工作的讚賞。

+0

他說他要檢查,如果地圖是圈子裏面,如果不圓是在地圖內。對我來說沒有多大意義,但我知道什麼? :) – Marcelo

+0

ohhhhhhhhhhhhhhhhhhhhh –

0

謝謝Tina 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"); 
    } 
}); 
0

這是一個老問題 - 在互聯網多年的恐龍歲 - 但如果你比較兩個邊界,然後下式成立更不用說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)) - 因此R中的所有(x,y)都包含在測試中。

我當然希望是這樣的話 - 這是我怎麼做我所有的界限比較...