I am new to Mapbox. I have created a custom style map in mapbox studio and then add the markers using geoJson. Here is the sample code:
mapboxgl.accessToken = 'pk.eyJ1Ijoic2Fua3ljc2Uhhcc.mb22KHuonjywQ-eaWQ';
var map = new mapboxgl.Map({
container: 'map_geo',
style: 'mapbox://styles/abcd/cipjtsdhyh04ebam5tndf4jaj',
zoom: 3.7,
center: [81.30, 22.76]
});
var geoJson = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"title": "Nagpur",
"description": "Nagpur",
"marker-symbol": "marker",
},
"geometry": {
"coordinates": [79.0882, 21.1845],
"type": "Point"
},
"id": "223e9579f03849c87abec10dfed64c37"
}, {
"type": "Feature",
"properties": {
"title": "Lucknow",
"description": "Lucknow",
"marker-symbol": "marker",
},
"geometry": {
"coordinates": [80.9462, 26.8467],
"type": "Point"
},
"id": "2cc757705489152c8bccb33635708427"
}]
};
map.on('load', function () {
map.addSource("markers", {
"type": "geojson",
"data": geoJson
});
map.addLayer({
"id": "markers",
"type": "symbol",
"source": "markers",
"layout": {
"icon-image": "{marker-symbol}-15",
"text-field": "{title}",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top",
},
"paint": {
"text-size": 16,
"text-color": "#fff",
}
});
});
map.scrollZoom.disable();
map.on('click', function (e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['markers'] });
if (!features.length) {
return;
}
// here I want to change the marker color to highlight the selected marker
});
Now I want to change the marker color on clicking any of the marker to display the selected marker. Thanks in advance.
Thanks for the question!
You will find this example helpful in implementing the functionality you've requested: https://www.mapbox.com/mapbox-gl-js/example/hover-styles/
The basic workflow is as follows:
click
eventqueryRenderedFeatures
We are working on ways to make this process more straightforward!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With