'Finding closest place of array list latitude long
Im trying to order the JSON from nearest to furthest when I put in my zipcode in an input element and now Im already able to get a list with the lat & lng when using the Geolib library but how can I use this with my JSON because I want to show the information in a list in my react application?
const getLatLngByZipcode = () => {
const geocoder = new google.maps.Geocoder();
const zipcode: string = inputEl.current!.value;
const placesArr: any = [];
geocoder.geocode({ address: zipcode }, (results, status) => {
if (status === google.maps.GeocoderStatus.OK) {
const lat: number = results[0].geometry.location.lat();
const lng: number = results[0].geometry.location.lng();
breweries.map((item) => placesArr.push(item.location));
const isWithinRange = orderByDistance(
{ latitude: lat, longitude: lng },
placesArr
);
console.log(isWithinRange);
} else {
alert('Request failed.');
}
});
};
{
"breweries": [
{
"address": "Overtocht 43",
"city": "Bodegraven",
"name": "Brouwerij de Molen",
"open": [
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
],
"zipcode": "2411 BT",
"location": {
"lat": 52.0856802,
"lng": 4.7420825
}
},
{
"address": "Weverij 5",
"city": "Amstelveen",
"name": "de naeckte brouwers",
"open": ["Wednesday"],
"zipcode": "1185 ZE",
"location": {
"lat": 52.2899968,
"lng": 4.840067599999999
}
},
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
