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

277
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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