0

I am relatively new to Mapbox GL JS and I am working on a project where I find to change the color and icon of certain locations on the map. I went to Mapbox studio and was able to obtain the specific id for those locations. However, I do not know how to match them in my JS code and change their colors and icons.

I am also trying to display a popup window that displays the information about those locations when the user clicks on the icons of the location. Also having a hard time matching the id and opening a popup window.

This is my current JS code:

const police_station = {
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "icon": "police-15",
            "color": "#004cff",
            "title": "City of Seattle Police Department",
            "description": "1519 12th Ave, Seattle, WA 98122"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-122.31725715100764,
                47.614924945431525
            ]
        }
    }]
}
// User can click on icon to comment and view information
for (const feature of police_station.features) {
    // create a HTML element for each feature
    const el = document.createElement('div');
    el.className = 'police';
    // make a marker for each feature and add to the map
    new mapboxgl.Marker(el).setLngLat(feature.geometry.coordinates).setPopup(new mapboxgl.Popup({
        offset: 25
    }).setHTML(`
                                      <h2>${feature.properties.title}</h2>
                                      <p>${feature.properties.description}</p>
                                      <button class="open-button" onclick="openForm()">Comment</button>`)).addTo(map);

CSS:

  .police {
    background-image: url('../assets/police.svg');
    background-size: cover;
    width: 25px;
    height: 25px;
    border-radius: 50%;
    cursor: pointer;
  }

Screenshot of Map

Let's suppose I want to change the icon and icon's color for the resturant Plum Bistro with an unique id of 12345, and have a popup windown displaying its information, how would I do so?

1 Answers1

0

you could add innterHTML to your el (marker). Check this:

// User can click on icon to comment and view information
for (const feature of police_station.features) {
    // create a HTML element for each feature
    const el = document.createElement('div');
    el.className = 'police';
    el.innerHTML = '<img src="YOUR URL TO A IMG ex: /assets/marker-on-map.svg">';
    // make a marker for each feature and add to the map