【问题标题】:GMaps: Cover all earth in polygon and make a hole in thatGMaps:用多边形覆盖所有地球并在其中打一个洞
【发布时间】:2012-01-31 23:36:51
【问题描述】:

我正在地图上绘制两个多边形。第二个多边形在第一个多边形上打了一个洞。我希望第一个多边形尽可能多地覆盖地球。因此,让我们专注于这一点,暂时放下这个漏洞。

因为 max/min lat = 90,-90 和 max/min lng = 180, -180。

如果我画下面的似乎“互相吃掉”

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];

如果我稍微调整一下这些值,我可以让它们不至于互相吃掉,但我总是有很大的失误。

提前致谢。

【问题讨论】:

  • “互相吃掉”是什么意思 - 你的意思是它们重叠吗?指向实时代码的链接会很有价值。
  • 感谢您抽出宝贵时间,但我现在设法修复了它。这将在整个地图上绘制一个多边形:points = [new google.maps.LatLng(-87, 120), new google.maps.LatLng(-87, -87), new google.maps.LatLng(-87, 0)];然后,您可以在其上绘制另一个多边形以打孔。

标签: google-maps google-maps-api-3


【解决方案1】:

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

<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> 

【讨论】:

  • 正是我需要的,triangleCorrds!
猜你喜欢
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 2021-04-20
  • 2016-11-06
相关资源
最近更新 更多