I have an option page where i can create maps, i created a custom field to paste my own styles. What i need to do is pass this informations in my maps.js. What i have done is create an hidden div like this (i'm using timber) :

<div class="hidden" id="map-styles">{{ options.map_styles }}</div>

Then in maps.js i get this informations like this :

function initMap($el) {

// Find marker elements within map.
var $markers = $el.find(".marker");
var styles = document.getElementById("map-styles").textContent;

// Create generic map.
var mapArgs = {
zoom: $el.data("zoom") || 16,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: styles,

But it's not working, however if i'm doing a console.log('styles'), i see my code and if i paste my code directly in my js file it's working. Do you guys have any idea of how to solve this ? Is it a formating issue or something like that, I would really appreciate if you could put me on the right track.

You can see the map here : http://thefabulous.odns.fr/


  • You should include a sample of what the map styles look like when you input them and what they look like when you output them. May 18, 2020 at 14:16

1 Answer 1


So I had a look at your site and found the json map styles in your div #map-styles.

However your json is not quite valid. Not sure where you copied the style source from or wether acf is manipulating the output.

I cleaned up your style json so it is valid, see validated version here.

So with regards to integrating it into your maps.js, the option style doesn't exist, you need to modify mapTypeControlOptions to get your style registered as a mapTypeId

See google mapTypeControlOptions docs here.

Here is what I added to your current script to get it working. Note this wont work unless you make sure your json is valid from your acf field.

// map args
var mapArgs = {
  zoom: $el.data("zoom") || 16,
  mapTypeControlOptions: {
    mapTypeIds: [
    style: google.maps.MapTypeControlStyle.HORIZONTAL

// set our map
var map = new google.maps.Map($el[0], mapArgs);

// get our json from #map-sytles div
var customStyle = JSON.parse($('#map-styles').text());

// create the custom styled map type object
var customStyleMap = new google.maps.StyledMapType(customStyle, {
  name: 'Custom Style'

// set the custom styled map type against the map
map.mapTypes.set('custom_map_style', customStyleMap);

You will notice that i've added your custom style map as another option in mapTypeIds.

You can remove the default google.maps.MapTypeId.ROADMAP or google.maps.MapTypeId.SATELLITE or both... from mapTypeIds. Always nice to keep the satellite option in my opinion.

Here is a working demo on jsfiddle using your code from your site with the above tweeks.


What would be cleaner is to output the json into a data attribute rather than a hidden div.

<div class="acf-map" data-zoom="19" data-style="{{ options.map_styles }}">

And get the json this way $el.data('style');

Hope this gets you on the right track.

  • 1
    Thank you so much! It works, 1000 thanks for your time.
    – loicdls
    May 24, 2020 at 16:28
  • No worries glad it worked for you.
    – joshmoto
    May 29, 2020 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.