Toggle marker color on click
Set an active marker color depending on the one selected.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Toggle marker color on click</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v3.3.1/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v3.3.1/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
L.mapbox.accessToken = '<your access token here>';
var map = L.mapbox.map('map')
.setView([37.9, -77], 6)
.addLayer(L.mapbox.styleLayer('mapbox://styles/mapbox/streets-v11'));
var geoJson = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-77, 37.9]
},
properties: {
title: 'Marker One',
'marker-color': '#bbb'
}
},
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-78, 36.5]
},
properties: {
title: 'Marker Two',
'marker-color': '#bbb'
}
}]
};
var myLayer = L.mapbox.featureLayer().addTo(map);
myLayer.setGeoJSON(geoJson);
function resetColors() {
for (var i = 0; i < geoJson.length; i++) {
geoJson[i].properties['marker-color'] = geoJson[i].properties['old-color'] ||
geoJson[i].properties['marker-color'];
}
myLayer.setGeoJSON(geoJson);
}
myLayer.on('click', function(e) {
resetColors();
e.layer.feature.properties['old-color'] = e.layer.feature.properties['marker-color'];
e.layer.feature.properties['marker-color'] = '#ff8888';
myLayer.setGeoJSON(geoJson);
});
map.on('click', resetColors);
</script>
Get a free Mapbox account to create your own custom map and use it in this example.
Use this example by copying its source into your own HTML page and
replacing the Map ID
with
one of your own from your projects.
Having trouble with JavaScript? Try out Codecademy
or contact our support team.