4

I am trying to draw a rectangle polygon with a hole. My problem is that I cannot create a polygon that covers the whole world. The polygon is inverted so that only a single line is selected instead of the whole world.

Below an example of the maximum selection I am able to make. For example if I try to alter 0 (in the line new google.maps.LatLng(-85.1054596961173,0)) to any other value the inverted selection is used.

Maybe I am missing something obvious here, but I can't seem to get it working.

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);
}

Here is the fiddle: http://jsfiddle.net/xs9fcdf9/4/

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Nebu
  • 1,753
  • 1
  • 17
  • 33
  • 1
    related question: [Change map opacity outside circle of Google Maps JavaScript API v3](http://stackoverflow.com/questions/19736418/change-map-opacity-outside-circle-of-google-maps-javascript-api-v3) – geocodezip Dec 11 '14 at 15:40

1 Answers1

13

You need to make the winding direction of the outer polygon opposite that of the inner polygon and add a point:

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

working code snippet:

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?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas" style="width:500px;height:500px;"></div>
geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • 1
    The above worldCoords do not work on iOS. Could you replace them with the coordinates you used in your other answer: https://stackoverflow.com/a/11130903/1222409? Thanks! – m.spyratos Nov 06 '18 at 05:57
  • i also confirm that they do not work on iOS 15. – IulianT Jul 08 '22 at 12:27