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

279
Vistas
how to searching for the closest location from closest to furthest at least 5 places ReactJS?

I'm making a nearby location search website. I'm trying to display the 5 closest locations starting from the closest to the farthest using a loop. but i get the same location result. I want 5 different locations and I'm still confused how to solve it. i use this package on npm map nearest location

 const [myLocation, setMylocation] = useState(null);
 const [status, setStatus] = useState(null);
 const [dapat, setDapat] = useState([]);

 useEffect(() => {
      getLocation();
 }, []);

 useEffect(async() => {
      const response = await axios.get(Urlfromdatabase);
      let locations = []
      response.data.map((user) => {
           locations.push({
                "name": user.name,
                "address": user.address,
                "phone": (user.phone).toString(),
                "facility": user.facility,
                "lat": Number(user.lat),
                "lng": Number(user.lng)
           })
           return locations
      })
      if (myLocation) {
      var nearestLocations = [];
           for (let step = 0; step < 5; step++) {
                if (locations.length === 0) break;
                let nearest = findNearestLocation(myLocation, locations);
                nearestLocations.push(nearest);
                locations = locations.filter(function (location) {
                     return (location.lat !== nearest.lat && location.lng !== nearest.lng);
                });
           }
           setDapat(nearestLocations);
           console.log(nearestLocations);
      }
 }, [myLocation]);

 const getLocation = () => {
      if (!navigator.geolocation) {
           setStatus('geolocation not support on your browser!!');
      } else {
           setStatus('locating...!!');
           navigator.geolocation.getCurrentPosition((position) => {
                setStatus(null);
                setMylocation({
                     lat: position.coords.latitude,
                     lng: position.coords.longitude
                })
           }, () => {
                setStatus('can't get the location!');
           });
      }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The library you linked only finds the nearest location (hence the name). The logic is trivial though, just modify the source to append to an array instead of updating a single closest var.

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