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

267
Vistas
Mapbox GL JS find closest address to clicked point

I am trying to make a web app where the user can click a point and I can get the closest address to the point. This example from the documentation looks pretty close to what I want to do, except it the queryRenderedFeatures call doesn't seem to return the physical address of any features. What is the best way to get the physical address from a clicked point?

Here is my code:

  map.on("click", (e) => {
    const features = map.queryRenderedFeatures(e.point);
    const displayProperties = [
      "type",
      "properties",
      "id",
      "layer",
      "source",
      "sourceLayer",
      "state",
    ];
    const displayFeatures = features.map((feat) => {
      const displayFeat = {};
      displayProperties.forEach((prop) => {
        displayFeat[prop] = feat[prop];
      });
      return displayFeat;
    });
    console.log(displayFeatures);
  });
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can't get the street address from the queryRenderedFeatures function. However, you can get the coordinates of the point clicked:

map.on("click", (e) => {
    mapClickFn(e.lngLat);
});

From these coordinates, you need to get a street address. This is called "reverse geocoding" and can be done using the Mapbox API:

function mapClickFn(coordinates) {
    const url =
      "https://api.mapbox.com/geocoding/v5/mapbox.places/" +
      coordinates.lng +
      "," +
      coordinates.lat +
      ".json?access_token=" +
      YOUR_ACCESS_TOKEN +
      "&types=address";
    $.get(url, function (data) {
      if (data.features.length > 0) {
        const address = data.features[0].place_name;
        console.log(address);
      } else {
        console.log("No address found");
      }
    });
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

The term for finding the nearest address to a point is "reverse geocoding". You could use Mapbox's API for this, but there are many others, too.

There is no need to use queryRenderedFeatures here.

about 4 years ago · Juan Pablo Isaza Denunciar
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