带孔的世界地图的多边形(谷歌地图)

时间:2014-12-10 10:45:21

标签: google-maps

我正在尝试绘制一个带孔的矩形多边形。我的问题是我无法创建覆盖整个世界的多边形。多边形被反转,因此只选择一条线而不是整个世界。

下面是我能够做出的最大选择的示例。例如,如果我尝试将0(在新的google.maps.LatLng(-85.1054596961173, 0 )行中更改为任何其他值,则使用反向选择。

也许我在这里遗漏了一些明显的东西,但我似乎无法让它发挥作用。

function initialize() {
  var mapOptions = {
    zoom: 1,
    center: new google.maps.LatLng(24.886436490787712, -70.2685546875),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };


  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  var worldCoords = [
    new google.maps.LatLng(-85.1054596961173, -180),
    new google.maps.LatLng(85.1054596961173, 180),
    new google.maps.LatLng(85.1054596961173, 180),
   new google.maps.LatLng(-85.1054596961173,0)

  ];

  var EuropeCoords = [
              new google.maps.LatLng(29.68224948021748, -23.676965750000022),
              new google.maps.LatLng(29.68224948021748, 44.87772174999998),
              new google.maps.LatLng(71.82725578445813, 44.87772174999998),
              new google.maps.LatLng(71.82725578445813, -23.676965750000022)
    ];


  // Construct the polygon.
  poly = new google.maps.Polygon({
    paths: [worldCoords,EuropeCoords],
    strokeColor: '#000000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#000000',
    fillOpacity: 0.35
  });


  poly.setMap(map);
}

这是小提琴:http://jsfiddle.net/xs9fcdf9/4/

1 个答案:

答案 0 :(得分:11)

您需要使外部多边形的缠绕方向与内部多边形的缠绕方向相反,并添加一个点:

var worldCoords = [
  new google.maps.LatLng(-85.1054596961173, -180),
  new google.maps.LatLng(85.1054596961173, -180),
  new google.maps.LatLng(85.1054596961173, 180),
  new google.maps.LatLng(-85.1054596961173, 180),
  new google.maps.LatLng(-85.1054596961173, 0)
];

working fiddle

工作代码段:

function initialize() {
    var mapOptions = {
        zoom: 1,
        center: new google.maps.LatLng(24.886436490787712, -70.2685546875),
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };


    var map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

    var worldCoords = [
    new google.maps.LatLng(-85.1054596961173, -180),
    new google.maps.LatLng(85.1054596961173, -180),
    new google.maps.LatLng(85.1054596961173, 180),
    new google.maps.LatLng(-85.1054596961173, 180),
    new google.maps.LatLng(-85.1054596961173, 0)];


    var EuropeCoords = [
    new google.maps.LatLng(29.68224948021748, -23.676965750000022),
    new google.maps.LatLng(29.68224948021748, 44.87772174999998),
    new google.maps.LatLng(71.82725578445813, 44.87772174999998),
    new google.maps.LatLng(71.82725578445813, -23.676965750000022)];


    // Construct the polygon.
    poly = new google.maps.Polygon({
        paths: [worldCoords, EuropeCoords],
        strokeColor: '#000000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#000000',
        fillOpacity: 0.35
    });

    poly.setMap(map);
}
initialize();
<script src="http://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas" style="width:500px;height:500px;"></div>

相关问题