在多边形中绘制多个洞 - 谷歌地图api

时间:2013-02-17 08:27:12

标签: google-maps

我使用与此示例http://www.geocodezip.com/v3_polygon_example_donut.html相同的方法在多边形中绘制我的初始整体。我现在想在初始整体之后在同一个多边形中绘制多个整体,所以我想我可以这样做:

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 个答案:

答案 0 :(得分: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>

相关问题