Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
Make jQuery events (mouseenter, mouseleave) handle Mapbox markers

I have a webpage with a Mapbox map embedded in it (the Mapbox map has been integrated with D3.js for some filtering fuctions which are not the topic of this thread).

Each marker has some related JSON (geojson) information (year, city etc.) and I want to make this information displayed inside a whenever the user hovers a marker. In this specific case it would be great to manage assigning a jQuery event (mouseenter, mouseleave) to the markers in order to display the content related to the marker.

For some reason I cannot handle markers as they are not in the markup and this is why I cannot assign jQuery events to them. Maybe this has to do with how the Mapbox map has been embedded on the page.

This is the JS Fiddle with the map.

This is the JS:

mapboxgl.accessToken = 'pk.eyJ1IjoicnNjaG1pZHQiLCJhIjoiY2ttZHozaXBlMWo3czJ2cWxlazBmYm9wbSJ9.VWwsnFgo2QdFd55Lm6wkVw';
var map = new mapboxgl.Map({
  container: 'map',
  style: 'mapbox://styles/rschmidt/ckn9czr4o34g417o2v1h9yh2x',
  center: [8.2, 46.9],
  zoom: 7.2
});

map.on('load', function () {
  d3.json(
      'https://fernsehfolklore.ch/wp-content/themes/soundfolklore/assets/build/json/features.geojson', 
      function (err, data) {
          if (err) throw err;

          data.features = data.features.map(function (d) {
              return d;
          });

          map.addSource('places', {
            type: 'geojson',
            data: locations, 
            generateId: true 
          });

      }
  );
});

locations.features.forEach(function(marker, i) {
  var el = document.createElement('div');
  el.id = "marker-" + i;
  el.setAttribute('data-index', i + 1);
  el.className = 'marker';
  new mapboxgl.Marker(el, {
        offset: [-36 / 2, -36] // NOTE: 36 equals .marker size
    })
    .setLngLat(marker.geometry.coordinates)
    .addTo(map);

  el.addEventListener('click', function(e){
    flyToLocation(marker);
    updateList(marker);
  });
});

Anyone how knows how to handle Mapbox markers?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda