I have a large amount of city pages on my blog where I need to put city-specific maps with additional control to build a route, on each city page.
This is the layout I'm looking to create:
I am using Mapbox GL JS library for the map. The JS files and styles for the map and directions control are hosted on Mapbox CDN:
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.0/mapbox-gl.js'></script>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v3.1.1/mapbox-gl-directions.js'></script>
<link rel='stylesheet' href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.0/mapbox-gl.css' />
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v3.1.1/mapbox-gl-directions.css' type='text/css' />
To put the map on the page, the following code is used:
<div id='map'></div>
<script>
mapboxgl.accessToken = 'YourAccessToken';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [-71.4512, 43.6568],
zoom: 13
});
map.addControl(new MapboxDirections({
accessToken: mapboxgl.accessToken
}), 'top-left');
</script>
Since I need to cover many cities, I have created a new template exactly for city pages. So I'm simply adding the code mentioned above right between header and page content / sidebar (please see the layout mockup).
The problem is that I don't know how to pass latitude / longitude values for each specific city/map to this portion of the code in Mapbox script:
center: [-71.4512, 43.6568]
Ideally if I can input them in Edit Page through custom fields:
and these two values will be somehow populated in the script. Maybe it's possible to add some kind of placeholders for these two parameters in page template:
center: [%latitude%, %longitude%]
so that they will be populated from corresponding fields, and specific city map is loaded. Would appreciate if someone can point me in the right direction on how to do it.
Thank you!

