2013-02-17 151 views
0

我使用与此示例相同的方法http://www.geocodezip.com/v3_polygon_example_donut.html在多边形中绘制我的初始整体。我现在要绘制多个整体而在相同的多边形后的首次全,所以我想我可能只是这样做:在多边形中绘制多个洞 - 谷歌地图api

var p = new google.maps.Polygon({ 
     paths: [drawCircle(getPosition(), 100, 1), 
     drawCircle(getPosition(), .02, -1), 
     drawCircle(new google.maps.LatLng((45.345573,-71.09909500000003), 
     10, -1))], 
     strokeColor: "#040102", 
     map: map 
    }); 

但是,这并不工作。数组中的前2个路径将被绘制(它看起来与圆环示例完全相同),但它不会渲染最后一个路径。任何想法为什么?

谢谢!

+1

这[示例](HTTP:/ /www.geocodezip.com/v3_polygon_example_donutA.html)绘制多个洞。 – geocodezip 2013-02-17 10:22:42

回答

0

你在你的代码一个错字(一组额外的括号)

var p = new google.maps.Polygon({ 
     paths: [drawCircle(getPosition(), 100, 1), 
     drawCircle(getPosition(), .02, -1), 
     drawCircle(new google.maps.LatLng(45.345573,-71.09909500000003), 
     10, -1)], 
     strokeColor: "#040102", 
     map: map 
    }); 

代码片段:

var map = null; 
 
var bounds = null; 
 

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

 
    bounds = new google.maps.LatLngBounds(); 
 

 
    var p = new google.maps.Polygon({ 
 
    paths: [ 
 
     drawCircle(getPosition(), 100, 1), 
 
     drawCircle(new google.maps.LatLng(45.4009928, -71.8824288), 10, -1), 
 
     drawCircle(new google.maps.LatLng(45.345573, -71.099095), 10, -1) 
 
    ], 
 
    strokeColor: "#040102", 
 
    map: map 
 
    }); 
 

 
    map.fitBounds(bounds); 
 

 
} 
 

 
function getPosition() { 
 
    return new google.maps.LatLng(45.345573, -71.099095); 
 
} 
 
google.maps.event.addDomListener(window, 'load', initialize); 
 

 
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 = 32; 
 

 
    // 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]); 
 
    } 
 
    return extp; 
 
}
body, 
 
html, 
 
#map_canvas { 
 
    margin: 0px; 
 
    padding: 0px; 
 
    width: 100%; 
 
    height: 100%; 
 
}
<script src="http://maps.google.com/maps/api/js"></script> 
 
<div id="map_canvas"></div>

+0

谢谢!我也只是注意到你的多个洞的例子。 :] – user1527634 2013-02-17 18:29:10

+0

我想从现有的多边形中减去一个多边形,你知道一种方法吗?这种方法的1和-1的方向性将工作,但现在它是这样做的:http://imgur.com/UwXPWTp - 是否有办法取消重叠的整个圈子?谢谢! – user1527634 2013-02-18 02:08:33

+0

这真是另一个问题。要减去,你需要扭转缠绕方向。 – geocodezip 2013-02-18 09:25:37