Skip to content

Instantly share code, notes, and snippets.

@philJohnson
Created February 2, 2021 07:09
Show Gist options
  • Save philJohnson/63d4dc49439fe37e373d33c257480325 to your computer and use it in GitHub Desktop.
Save philJohnson/63d4dc49439fe37e373d33c257480325 to your computer and use it in GitHub Desktop.
Setup Google map with custom styles
// import external dependencies
import forEach from 'lodash/forEach';
import styles from "./styles";
const init = (selector, callback) => forEach(document.querySelectorAll(selector), callback);
init('[data-map]', (el) => {
let map;
const initMarker = (el) => {
// Get position from marker.
const markerGeo = JSON.parse(el.dataset.mapMarker);
var latLng = {
lat: parseFloat( markerGeo.lat ),
lng: parseFloat( markerGeo.lng ),
};
// Create marker instance.
var marker = new window.google.maps.Marker({
position : latLng,
map: map,
});
// Append to reference for later use.
map.markers.push( marker );
}
var markers = el.querySelectorAll('[data-map-marker]')
const geo = JSON.parse(el.dataset.map);
map = new window.google.maps.Map(el, {
center: { lat: parseFloat(geo.lat), lng: parseFloat(geo.lng) },
zoom: 16,
styles: styles,
disableDefaultUI: true,
});
map.markers = [];
forEach(markers, initMarker);
return map;
})
{% if post.map %}
<div class="wp-block-google-map alignfull" data-map={{ { 'lat' : post.map.lat, 'lng' : post.map.lng } | json_encode() }}>
<div data-map-marker={{ { 'lat' : post.map.lat, 'lng' : post.map.lng } | json_encode() }}></div>
</div>
{% endif %}
[
{
"featureType": "all",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#ffffff"
}
]
},
{
"featureType": "all",
"elementType": "labels",
"stylers": [
{
"visibility": "on"
}
]
},
{
"featureType": "all",
"elementType": "labels.text.fill",
"stylers": [
{
"saturation": "0"
},
{
"lightness": "66"
}
]
},
{
"featureType": "all",
"elementType": "labels.text.stroke",
"stylers": [
{
"color": "#ffffff"
},
{
"gamma": "1"
},
{
"weight": "4.44"
},
{
"visibility": "off"
}
]
},
{
"featureType": "all",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "simplified"
},
{
"saturation": "-100"
},
{
"gamma": "0.41"
},
{
"weight": "0.01"
},
{
"lightness": "61"
}
]
},
{
"featureType": "administrative.land_parcel",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "landscape",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "landscape",
"elementType": "geometry.fill",
"stylers": [
{
"visibility": "on"
},
{
"color": "#272729"
}
]
},
{
"featureType": "poi",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#272729"
},
{
"lightness": "0"
},
{
"gamma": "1"
}
]
},
{
"featureType": "poi",
"elementType": "geometry.stroke",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "poi.attraction",
"elementType": "all",
"stylers": [
{
"color": "#ff0000"
},
{
"visibility": "off"
}
]
},
{
"featureType": "poi.attraction",
"elementType": "geometry.fill",
"stylers": [
{
"visibility": "on"
},
{
"color": "#272729"
}
]
},
{
"featureType": "poi.business",
"elementType": "all",
"stylers": [
{
"visibility": "simplified"
},
{
"gamma": "0.00"
},
{
"saturation": "-59"
},
{
"lightness": "20"
}
]
},
{
"featureType": "poi.business",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#ffffff"
},
{
"lightness": "10"
},
{
"saturation": "-29"
}
]
},
{
"featureType": "poi.business",
"elementType": "labels.icon",
"stylers": [
{
"saturation": "74"
},
{
"lightness": "-25"
},
{
"gamma": "0.45"
},
{
"hue": "#ff002c"
}
]
},
{
"featureType": "poi.government",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "poi.medical",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#272729"
},
{
"visibility": "off"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "poi.place_of_worship",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "poi.school",
"elementType": "all",
"stylers": [
{
"visibility": "off"
},
{
"color": "#ff0000"
}
]
},
{
"featureType": "poi.sports_complex",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"color": "#121213"
}
]
},
{
"featureType": "road",
"elementType": "geometry.stroke",
"stylers": [
{
"color": "#ffffff"
},
{
"weight": "5.81"
},
{
"visibility": "off"
}
]
},
{
"featureType": "road",
"elementType": "labels.text.fill",
"stylers": [
{
"visibility": "on"
},
{
"color": "#4e4e4e"
},
{
"lightness": "17"
},
{
"saturation": "-39"
}
]
},
{
"featureType": "road",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit",
"elementType": "geometry",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#a89473"
},
{
"visibility": "off"
}
]
},
{
"featureType": "transit",
"elementType": "geometry.stroke",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit.line",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit.station.rail",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "water",
"elementType": "all",
"stylers": [
{
"color": "#0b0b0b"
},
{
"visibility": "simplified"
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment