How to Style Google Maps

You can use styled Google Maps to customize the visual display of maps. Here's how:

For this tutorial, you can start with the Google map you created in 'How to Create a Simple Google Maps Mashup':


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
	<style>
		#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;
	var parks = [
		{"park":"Yellowstone","coords":[44.4280,-110.5885]},
		{"park":"Glacier","coords":[48.7596, -113.7870]}
		];

	function initMap() {
		map = new google.maps.Map(document.getElementById('map'), {
			zoom: 4,
			center: new google.maps.LatLng(44, -110),
			mapTypeId: 'terrain'
		});

		mashParks(parks);
	}

	function mashParks(results) {
		for (var i = 0; i < results.length; i++) {
			var coords = results[i].coords;
			var latLng = new google.maps.LatLng(coords[0], coords[1]);
			var marker = new google.maps.Marker({
				position: latLng,
				map: map,
				label:results[i].park
			});
		}
	}

</script>
<script async defer
		src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDTM5RKrtCLpZAj5HU3K3cTvFNB240NsBg&callback=initMap">
</script>
</body>
</html>

To apply style to a map, you must change the styles of the standard map type by modifying the .styles property of the MapOptions object. To do this, you'll use an array of MayTypeStyle objects. Each array uses selectors and styles to customize the display of your map. Starting with your basic, unstyled, map, follow these steps:

  1. Create an array named stylesArray inside the scripts block in your map page. This array should contain at least one MapTypeStyle object, which will have a stylers array and, optionally, a featureType property and an elementType property.
  2. 
    var stylesArray = [
    	{
    		featureType: '',
    		elementType: '',
    		stylers: [
    			{color: ''},
    			{visibility: ''},
    		]
    	},
    	{
    		featureType: '',
    		elementType: '',
    		stylers: [
    			{color: ''},
    			{visibility: ''},
    		]
    	}
    ];
    
  3. Modify the values of the properties to customize your map. You can find a complete reference to the available properties and values at https://developers.google.com/maps/documentation/javascript/style-reference. Here's an example:
  4. 
    var stylesArray = [
    	{
    		featureType: 'poi.park',
    		elementType: 'geometry',
    		stylers: [
    			{color: '#00FF00'}
    		]
    	},
    	{
    		featureType: 'water',
    		elementType: 'geometry',
    		stylers: [
    			{color: '#000000'}
    		]
    	}
    ];
    
  5. Pass stylesArray into the mapOptions object's styles property:
  6. 
    map = new google.maps.Map(document.getElementById('map'), {
    	zoom: 6,
    	center: new google.maps.LatLng(44, -110),
    	mapTypeId: 'terrain',
    	styles: stylesArray
    });
    
  7. Open your web page in a browser to see the results.
    Styled U.S. Parks Mashup
Author: Chris Minnick

Chris Minnick, the co-founder of WatzThis?, has overseen the development of hundreds of web and mobile projects for customers from small businesses to some of the world’s largest companies. A prolific writer, Chris has authored and co-authored books and articles on a wide range of Internet-related topics including HTML, CSS, mobile apps, e-commerce, e-business, Web design, XML, and application servers. His published books include Adventures in Coding, JavaScript For Kids For Dummies, Writing Computer Code, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW E-Commerce Designer Certification Bible, and XHTML.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.