Search code examples
javascriptgoogle-mapsgoogle-maps-api-3geojson

Removing outline and increasing opacity in GeoJSON drawn polygons on Google Maps API


I am working on something that leverages the Google Maps API to draw n number of polygons using GeoJSON. I was successful in drawing it, but I also want to increase the opacity of the polygons drawn, as well as remove an ugly outline on each of the polygons.

I looked through the documentation on the Google Maps API here, but it only tells you how to load the GeoJSON file, not modify traits of the drawn polygons.

map.data.loadGeoJson('google.json');

That up there is how you load the GeoJSON and the only command that you can use. I know it seems that I haven't tried anything, but I have and none of it's substantial enough to include in this question.

So my question is - How do you remove the outline from GeoJSON drawn images and also increase the opacity?

Below is also an image of what it currently looks like:

Image of what it currently looks like


Solution

  • use fillOpacity: 1 and strokeWeight: 0 in style options

    https://plnkr.co/edit/puMK8mAskLiaExmNKIEI?p=preview

    <!DOCTYPE html>
    <html>
      <head>
        <title>Data Layer: Styling</title>
        <meta name="viewport" content="initial-scale=1.0">
        <meta charset="utf-8">
        <style>
          /* Always set the map height explicitly to define the size of the div
           * element that contains the map. */
          #map {
            height: 100%;
          }
          /* Optional: Makes the sample page fill the window. */
          html, body {
            height: 100%;
            margin: 0;
            padding: 0;
          }
        </style>
      </head>
      <body>
        <div id="map"></div>
        <script>
    
          var map;
          function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
              zoom: 4,
              center: {lat: -28, lng: 137}
            });
    
            // Load GeoJSON.
            map.data.loadGeoJson(
                'https://storage.googleapis.com/mapsdevsite/json/google.json');
    
            // Set the stroke width, and fill color for each polygon
            map.data.setStyle(function(feature) {
              var color = feature.getProperty('color');
              return {
                fillColor: color,
                fillOpacity: 1,
                strokeWeight: 0
              };
            });
          }
        </script>
        <script async defer
        src="https://maps.googleapis.com/maps/api/js?callback=initMap">
        </script>
      </body>
    </html>