Web Tutorials

Interview Q & A

Code Examples

Utility Tools

Add Custom Style on Google Maps

In this example you will learn how to add custom style on map.

Here you can view the output of the example and you can also "try it yourself" by clicking on "Live Demo" button given at the bottom.

Syntax

//Loading google maps asynchronously
<script src="async defer src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initialize""> </script>  

//Create google map container
<div id="map"></div>

//Add custom style
var customStyleMap = new google.maps.StyledMapType([
      {
        stylers: [
          {hue: '#890000'},
          {visibility: 'simplified'},
          {gamma: 0.5},
          {weight: 0.5}
        ]
      },
      {
        elementType: 'labels',
        stylers: [{visibility: 'off'}]
      },
      {
        featureType: 'water',
        stylers: [{color: '#890000'}]
      }
    ], {
      name: 'Custom Style'
  });
  var customMapStyle = 'custom_style';
  
 var customMapTypeId = 'custom_style';
 var lat_lng = {lat: 20.08672, lng: 78.42444};
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 6,
    center: lat_lng,  // Brooklyn.
    mapTypeControlOptions: {
      mapTypeIds: [google.maps.MapTypeId.ROADMAP, customMapTypeId]
    }
  });

  map.mapTypes.set(customMapTypeId, customMapType);
  map.setMapTypeId(customMapTypeId);

Add Custom Style on Google Maps Example

<!DOCTYPE html>    
<html>    
<head><!-- www.techstrikers.com -->   
<title>Add Custom Style on Map</title>  
  <style>  
 html, body {    
    height: 100%;    
    margin: 0;    
    padding: 0;    
  }    
  #map {    
    height: 99%;   
     width: 99%;  
  }  
</style>  
//loading google maps asynchronously  
<script async defer src="https://maps.googleapis.com/maps/api/js?
signed_in=true&callback=initialize"> </script>   
 <script>  
function initialize() {
  var customMapType = new google.maps.StyledMapType([
      {
        stylers: [
          {hue: '#890000'},
          {visibility: 'simplified'},
          {gamma: 0.7},
          {weight: 0.5}
        ]
      },
      {
        elementType: 'labels',
        stylers: [{visibility: 'on'}]
      },
      {
        featureType: 'water',
        stylers: [{color: '#18A9DE'}]
      }
    ], {
      name: 'Custom Style'
  });
  var customMapTypeId = 'custom_style';
 var lat_lng = {lat: 20.08672, lng: 78.42444};
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 6,
    center: lat_lng,  // Brooklyn.
    mapTypeControlOptions: {
      mapTypeIds: [google.maps.MapTypeId.ROADMAP, customMapTypeId]
    }
  });

  map.mapTypes.set(customMapTypeId, customMapType);
  map.setMapTypeId(customMapTypeId);
}  
</script>  
</head>  
<body>  
<div id="map"></div>  
</body>  
</html> 

Above example will produce following output