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:
- Create an array named
stylesArray
inside the scripts block in your map page. This array should contain at least oneMapTypeStyle
object, which will have astylers
array and, optionally, afeatureType
property and anelementType
property. - 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:
- Pass stylesArray into the
mapOptions
object'sstyles
property: - Open your web page in a browser to see the results.
var stylesArray = [
{
featureType: '',
elementType: '',
stylers: [
{color: ''},
{visibility: ''},
]
},
{
featureType: '',
elementType: '',
stylers: [
{color: ''},
{visibility: ''},
]
}
];
var stylesArray = [
{
featureType: 'poi.park',
elementType: 'geometry',
stylers: [
{color: '#00FF00'}
]
},
{
featureType: 'water',
elementType: 'geometry',
stylers: [
{color: '#000000'}
]
}
];
map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: new google.maps.LatLng(44, -110),
mapTypeId: 'terrain',
styles: stylesArray
});