Mapbox is being used across the site, however, isn't loading on two specific pages?
There are three maps, all of which display fine on this page. And here is a working example of the page I am looking to replicate elsewhere, only with a different map. However, on this page the map doesn't load?
This is the javascript I'm using, each map has a different var
, stroud
, gloucester
and brimscombe
.
mapboxgl.accessToken = 'validtokenhere';
// STROUD
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"message": "Visit our Stroud clinic",
"iconSize": [30, 40]
},
"geometry": {
"type": "Point",
"coordinates": [
-2.248550,
51.741290
]
}
},
]
};
var stroud = new mapboxgl.Map({
container: 'stroud',
style: 'mapbox://styles/mapbox/light-v9',
center: [-2.248550,51.741290],
zoom: 13
});
// disable map zoom when using scroll
stroud.scrollZoom.disable();
// add markers to map
geojson.features.forEach(function(marker) {
// create a DOM element for the marker
var el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(/assets/img/marker.png)';
el.style.width = marker.properties.iconSize[0] + 'px';
el.style.height = marker.properties.iconSize[1] + 'px';
el.addEventListener('click', function() {
window.alert(marker.properties.message);
});
// add marker to map
new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.addTo(stroud);
});
// GLOUCESTER
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"message": "Visit our Gloucester clinic",
"iconSize": [30, 40]
},
"geometry": {
"type": "Point",
"coordinates": [
-2.248140,
51.870560
]
}
},
]
};
var gloucester = new mapboxgl.Map({
container: 'gloucester',
style: 'mapbox://styles/mapbox/light-v9',
center: [-2.248140,51.870560],
zoom: 13
});
// disable map zoom when using scroll
gloucester.scrollZoom.disable();
// add markers to map
geojson.features.forEach(function(marker) {
// create a DOM element for the marker
var el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(/assets/img/marker.png)';
el.style.width = marker.properties.iconSize[0] + 'px';
el.style.height = marker.properties.iconSize[1] + 'px';
el.addEventListener('click', function() {
window.alert(marker.properties.message);
});
// add marker to map
new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.addTo(gloucester);
});
// BRIMSCOMBE
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"message": "Visit our Brimscombe clinic",
"iconSize": [30, 40]
},
"geometry": {
"type": "Point",
"coordinates": [
-2.063020,
51.892550
]
}
},
]
};
var brimscombe = new mapboxgl.Map({
container: 'brimscombe',
style: 'mapbox://styles/mapbox/light-v9',
center: [-2.063020,51.892550],
zoom: 13
});
// disable map zoom when using scroll
brimscombe.scrollZoom.disable();
// add markers to map
geojson.features.forEach(function(marker) {
// create a DOM element for the marker
var el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(/assets/img/marker.png)';
el.style.width = marker.properties.iconSize[0] + 'px';
el.style.height = marker.properties.iconSize[1] + 'px';
el.addEventListener('click', function() {
window.alert(marker.properties.message);
});
// add marker to map
new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.addTo(brimscombe);
});
and then each container is
<section class="location-map-image">
<div id="stroud" class="map" style="width: 100%; height: 263px;"></div>
</section>
obviously the div id changes to match the location I wish to display.
I am expecting to see Gloucester and Brimscombe load like Stroud does?
Console log is showing
Error: Container 'stroud' not found. map.js:337:22
r map.js:337
<anonymous> app-min.js:12508
So it appears that the script gets stuck when it cannot find the div id stroud
. What should I be doing when I only want to show gloucester
or brimscombe
?