Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
MapboxGL Marker Popup elements do not exist until the popup is toggled

In the webapp that I am currently developing I show a number of locations around a city square with markers. When tapped/clicked the markers display a popup with a 4 item menulist of actions - e.g. More Information, View Gallery etc. On a typical city square I may show between 5 and 25 such markers.

The markers are created from JavaSript after the map of the square has been displayed. The code goes something like this

const element = document.createElement('div');
element.classList.add('popim000','popimNN');
//popimNN where NN is 01, 02 etc determines the map pin used

const marker = new mapboxgl.Marker({element}).setLngLat(entry.latlng),
      popup = new mapboxgl.Popup({offset:25}),
      html = '<span class='laSpan xpop'>Read More</span>' + 
             '<span class='laSpan xpop'>View Gallery</span>' + 
             '<span class='laSpan xpop'>Not of interest</span>';


marker.addTo(mapBoxMapInstance);
popup.setHTML(html);
marker.setPopup(popup);

I have simplified and formatted the actual code in the interests of readability. The problem I have run into is this. I want to detect mousedown and touchstart events on the spans above in order to do two things

  1. Hide the popup
  2. Display another HTML popup (not MapBox) that performs the required action - showing more information, displaying a picture gallery etc.

In order to do this I thought I could simply identify all relevant spans by issuing a document.getElementsByClassName('xpop'). However, this does not work - it looks like MapBox does not actually render the popup HTML into the DOM until it has, well, popped up.

I can circumvent the problem by doing this

  • First issue a marker.togglePopup() on each marker as it is generated and after it has been configured + attached to its parent map
  • Then grab a reference to each xpop span while the popup is still on display.

This works but feels like a very hackish solution to me. Is there another way to accomplish the same end result - linking the popup HTML mousedown and touchstart events with my own event handlers?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda