0

I am using this function below from this library Leaflet.GeometryUtil to help find the closest distance between two points.

    L.GeometryUtil,closest(map, array, latlng)

It is returning this object below, I can't find this lat and lng in the array I am checking over to see which one is closest to my starting point.

Could someone explain where the lat and lng comes from?

    {lat: -85.0511287798, lng: 32.577928810608014, distance: 3049.2708964603326}

This is what I have done -

  • Is your array a line\polygon? Are those coordinates on that line/polygon? From the documentation it seems that it simply returns the closest point on the line\polygon, that point might not be a vertex. – Dror Bogin Jul 1 at 15:47
1

Problem is that you mixed up latitude and longitude when reading from data source http://api.spitcast.com/api/spot/all. If you exchange coordinates, it's OK.

Relevant part of code should look like this:

//call function to get all surf spots
function getAllSpots() {
  fetch("http://api.spitcast.com/api/spot/all", {
    method: "GET"
  })
  .then(res => res.json())
  .then(json => {
    json.map(item => {
      var markers = {
        lon: item.longitude,
        lng: item.latitude,
        spot_name: item.spot_name,
        county_name: item.county_name
      };
      var coords = [item.latitude, item.longitude];
      spotsArray.push(markers);
      allCoords.push(coords);

    });       
    findDistances(allCoords)
  })
}

function findDistances(spotsArray) {
  console.log("allCoords", allCoords)
  let closest_latlng = L.GeometryUtil.closest(map, allCoords, [myLocation.lat, myLocation.lon])
  console.log(closest_latlng)
  L.marker([closest_latlng.lat, closest_latlng.lng]).addTo(map)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.