2012-01-31 22 views
1

我在地图上绘制两个多边形。第二个多边形在第一个中创建一个洞。我希望第一个多边形尽可能覆盖地球。所以让我们专注于这一点,并放下这个洞。由于最大/最小经度= 90,最大/最小经度= 180,-180。GMaps:覆盖多边形中的所有地球,并在那个洞做一个洞

如果我得出以下的似乎“吃对方了”

nw = new google.maps.LatLng(90, -180, true); 
ne = new google.maps.LatLng(90, 180, true); 
se = new google.maps.LatLng(-90, 180, true); 
sw = new google.maps.LatLng(-90, -180, true); 

points = [nw, ne, se, sw]; 

如果我调整值一点,我可以让他们不能吃起来对方,但我总是留下相当一个大错过。

在此先感谢。

+0

你说的“吃对方起来”的意思 - 你的意思是他们重叠?一个链接到现场代码将是有价值的。 – andresf 2012-02-01 01:17:09

+1

感谢您抽出时间,但我现在设法解决它。这将在整个地图上绘制一个多边形: points = [new google.maps.LatLng(-87,120),new google.maps.LatLng(-87,-87),new google.maps.LatLng(-87 ,0)]; 然后,您可以在其上绘制另一个多边形来打孔。 – Olle 2012-02-01 06:06:27

回答

2

我得到它的工作,这里是代码:

<script type="text/javascript" 
src="http://maps.google.com/maps/api/js?sensor=false"> 
</script> 
<script type="text/javascript"> 
function drawCircle(point, radius, dir) { 
var d2r = Math.PI/180; // degrees to radians 
var r2d = 180/Math.PI; // radians to degrees 
var earthsradius = 3963; // 3963 is the radius of the earth in miles 

    var points = 1000; 

    // find the raidus in lat/lon 
    var rlat = (radius/earthsradius) * r2d; 
    var rlng = rlat/Math.cos(point.lat() * d2r); 


    var extp = new Array(); 
    if (dir==1) {var start=0;var end=points+1} // one extra here makes sure we connect the 
    else  {var start=points+1;var end=0} 
    for (var i=start; (dir==1 ? i < end : i > end); i=i+dir) 
    { 
     var theta = Math.PI * (i/(points/2)); 
     ey = point.lng() + (rlng * Math.cos(theta)); // center a + radius x * cos(theta) 
     ex = point.lat() + (rlat * Math.sin(theta)); // center b + radius y * sin(theta) 
    extp.push(new google.maps.LatLng(ex, ey)); 
    bounds.extend(extp[extp.length-1]); 
    } 
    // alert(extp.length); 
    return extp; 
    } 

var map = null; 
var bounds = null; 

function initialize() { 
    var myOptions = { 
    zoom: 10, 
    center: new google.maps.LatLng(42,-80), 
    mapTypeControl: true, 
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, 
    navigationControl: true, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
          myOptions); 

    bounds = new google.maps.LatLngBounds(); 

    var donut = new google.maps.Polygon({ 
      paths: [triangleCoords = [ 
         new google.maps.LatLng(-87, 120), 
         new google.maps.LatLng(-87, -87), 
         new google.maps.LatLng(-87, 0)], 
        drawCircle(new google.maps.LatLng(42,-80), 1000, -1)], 
      strokeColor: "#000000", 
      strokeOpacity: 0.6, 
      strokeWeight: 2, 
      fillColor: "#999999", 
      fillOpacity: 0.6 
}); 
donut.setMap(map); 

map.fitBounds(bounds); 



} 

</script> 
+0

正是我需要的,triangleCorrds! – flyclassic 2012-07-30 08:30:19