Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!