Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use an image or a CSS gradient for filling of water areas in Google Maps?

I'm currently working on some project and need to implement the next design to a Google Map.Google Map design

I didn't manage to find any clue to answering of this question in Google Maps style reference - https://developers.google.com/maps/documentation/javascript/style-reference

So, I'm wondreing - is it possible at all (using only API)?

Or some hack is needed? I've thought, for example, about making of water areas transparent and placing the dotted image behind the map.

Thanks in advance!

like image 661
Dan Dev Avatar asked Jun 30 '17 02:06

Dan Dev


People also ask

What are the three main types of gradients in CSS?

You can choose between three types of gradients: linear (created with the linear-gradient() function), radial (created with the radial-gradient() function), and conic (created with the conic-gradient() function).

Is it possible to combine a background image and CSS3 gradients?

You can combine a background-image and CSS3 gradient on the same element by using several backgrounds. In our example below, we set the height and width of our <div> and add a background. Then, we set the background image fallback using the “url” value of the background-image property.

Can you do gradients in CSS?

CSS gradients let you display smooth transitions between two or more specified colors. CSS defines three types of gradients: Linear Gradients (goes down/up/left/right/diagonally) Radial Gradients (defined by their center)

Is Google Maps static or dynamic?

The Google Maps Platform static web APIs let you embed a Google Maps image on your web page without requiring JavaScript or any dynamic page loading. The APIs create an image based on URL parameters sent through a standard HTTP request and allow you to display the result on your web page.


1 Answers

In this answer, I will treat about turning off the visibility for water geometry on the map and to set background color behind the map.

First, as precised in the previous answer, you have to turn off the visibility of the map. You can consult the google documentation about stylers but the code is very simple:

styles: [{
              "elementType": "geometry.fill",
                  "stylers": [{
                  "visibility": "off"
              }]
          }, {
              "featureType": "landscape.natural.landcover",
                  "elementType": "geometry.fill",
                  "stylers": [{
                  "visibility": "on"
              }]
          }]

Here you declare in the mapOptions of your map that you want, first, to turn off the visibility of the water + the land and, in second, to turn "on" the visibility of the land.

function initialize() {
          var mapOptions = {
		          zoom: 2,
              center: new google.maps.LatLng(43.27749, -92.658775),
              styles: [{
                  "elementType": "geometry.fill",
                      "stylers": [{
                      "visibility": "off"
                  }]
              }, {
                  "featureType": "landscape.natural.landcover",
                      "elementType": "geometry.fill",
                      "stylers": [{
                      "visibility": "on"
                  }]
              }]
          };
          map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);
      }

      google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&.js"></script>
<div id="map_canvas" style="height:300px;"></div>

Second, you have to remove the default background color of the map (in gray). This question was already answered in Stack Overflow. You can simply use the parameter mapOptions of your map with the code below:

backgroundColor: 'none'

Which give:

var mapOptions = {
              backgroundColor: 'none',
          zoom: 2,
          center: new google.maps.LatLng(43.27749, -92.658775),
          styles: [{
              "elementType": "geometry.fill",
                  "stylers": [{
                  "visibility": "off"
              }]
          }, {
              "featureType": "landscape.natural.landcover",
                  "elementType": "geometry.fill",
                  "stylers": [{
                  "visibility": "on"
              }]
          }]
      };
      map = new google.maps.Map(document.getElementById('map_canvas'),
      mapOptions);

A simple example:

function initialize() {
          var mapOptions = {
		          backgroundColor: 'none',
              zoom: 2,
              center: new google.maps.LatLng(43.27749, -92.658775),
              styles: [{
                  "elementType": "geometry.fill",
                      "stylers": [{
                      "visibility": "off"
                  }]
              }, {
                  "featureType": "landscape.natural.landcover",
                      "elementType": "geometry.fill",
                      "stylers": [{
                      "visibility": "on"
                  }]
              }]
          };
          map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);
      }

      google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&.js"></script>
<div id="map_canvas" style="height:300px;"></div>

Finally, the code snippet below is a very simple example of this. There is a gradient color behind the map, the default background color of the map is removed and the visibility of the water is turn off.

 function initialize() {
          var mapOptions = {
		          backgroundColor: 'none',
              zoom: 2,
              center: new google.maps.LatLng(43.27749, -92.658775),
              styles: [{
                  "elementType": "geometry.fill",
                      "stylers": [{
                      "visibility": "off"
                  }]
              }, {
                  "featureType": "landscape.natural.landcover",
                      "elementType": "geometry.fill",
                      "stylers": [{
                      "visibility": "on"
                  }]
              }]
          };
          map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);
      }

      google.maps.event.addDomListener(window, 'load', initialize);
.gradient {
    height: 200px;
    background: -webkit-repeating-linear-gradient(white, blue); /* For Safari 5.1 to 6.0 */
    background: -o-repeating-linear-gradient(white, blue); /* For Opera 11.1 to 12.0 */
    background: -moz-repeating-linear-gradient(white, blue); /* For Firefox 3.6 to 15 */
    background: repeating-linear-gradient(white, blue); /* Standard syntax (must be last) */
}
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&.js"></script>
<div id="map_canvas" class="gradient" style="height:300px;"></div>

Note that this result is inspired of two posts. The first is the answer above and the second is the answer of Roman in this address in Stack Overflow.

like image 164
SphynxTech Avatar answered Oct 05 '22 23:10

SphynxTech