In my react native app I have used geolib to find the nearest place to a certain point from a list of places, like this
const nearestPlace = geolib.findNearest({ latitude:6.4423456, longitude:75.9095818 }, (places));
once I console log nearestPlace I get one latitude, longitude pair but when I try to get the value of the latitude like this,
const latitudeValue = nearestPlace.latitude
It's giving me an error saying property latitude does not exist on type 'GeolibInputCoordinates' so I can't get the latitude value.
Any Idea how to solve this?
I'd suggest you use the getLatitude, getLongitude functions. This will give you values for the latitude and longitude.
Perhaps your places array is in a different format such as { lat, lon } and this is what is causing the issue. They may not necessarily have a latitude or longitude property.
const places = [{ lat: 6.45, lon: 75.90 }, { lat: 6.44, lon: 75.91 }];
let nearestPlace = geolib.findNearest({ latitude: 6.4423456, longitude: 75.9095818 }, (places));
console.log('Nearest place:', nearestPlace);
const latitudeValue = geolib.getLatitude(nearestPlace);
const longitudeValue = geolib.getLongitude(nearestPlace);
console.log('Latitude:', latitudeValue);
console.log('Longitude:', longitudeValue);
// You can also 'normalize' this like so:
const point = { latitude: latitudeValue, longitude: longitudeValue};
console.log('Nearest point (normalized):', point);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://unpkg.com/geolib@3.3.1/lib/index.js"></script>