0

I'm using geolib to get Point is within the circle, and the below function returns only TRUE/FALSE.

geolib.isPointWithinRadius(
    { latitude: 51.525, longitude: 7.4575 }, // My current location.
    { latitude: 51.5175, longitude: 7.4678 }, // returns TRUE if this point is within 5 km radius from my current location.
    5000
);

and yes it's working. But what i want is, i want to check multiple locations. for ex, i have below array, and i want to check the same function to check all the locaions, if any one of location is within 5 km from my locaion it should return TRUE. in other words, i will be getting FALSE only when none of the locations are within 5 km of my current location

const markers = [
    {
      title: "my location1",
      coordinates: {
        latitude: 14.599912,
        longitude: 24.1147557,
      },
    },
    {
      title: "my location 2",
      coordinates: {
        latitude: 34.599912,
        longitude: 44.1147557,
      },
    },
    {
      title: "my location 3",
      coordinates: {
     latitude: 54.599912,
        longitude: 64.1147557,
      },
    },
  ];

I tried this,

geolib.isPointWithinRadius(
    { latitude: 51.525, longitude: 7.4575 },
    markers.map((marker) => marker.coordinates),
    50
  );

i get only false, i would really appreciate your help. Thanks in advance.

1 Answer 1

2

The some method can be used to check if at least one item returns true:

const isWithinRange = markers.some(marker => {
  return geolib.isPointWithinRadius(
    { latitude: 51.525, longitude: 7.4575 },
    marker.coordinates,
    50
  );
})

docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Sign up to request clarification or add additional context in comments.

8 Comments

Great, it actually worked, but its says as a warning Each child in a list should have a unique key prop
That warning occurs if you use map in your render method and don't give each item a value for the key prop. The key should be something that uniquely identifies the row. like: items.map(item => <div key={item.id}>{item.name} {item.price}</div>) or you can use the index if don't have an unique identifier: items.map((item, index) => <div key={index}>{item.name}</div>. Check the docs for more details: reactjs.org/docs/lists-and-keys.html#keys
yes, i got it, but here it returns just a function. where ever i use key prop inside the function, i am getting error. i really hope u would give a hand on this.
Could you further clarify "key prop inside the function"? The key prop should be added to the element?
No.. the answer you gave me just exactly what i want, i just getting this warning go away. i dont need a key prop in element.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.