I'm filtering the number of cities in a JSON file that are within 10 miles (16 km) of Los Angeles (LA).
I can retrieve the latitude and longitude of LA no problem, but I need to use "latLng.lat" and "latLng.lng" in my "filtered" variable.
The error shows:
Uncaught TypeError: Cannot read property 'latLng' of undefined
Code
var location = 'Los Angeles';
const geocoder = new google.maps.Geocoder()
let city = location + ',us';
var latLng = {};
geocoder.geocode({
address: city
}, function(results, status) {
if (status === 'OK') {
const result = results[0].geometry.location
const lat = result.lat()
const lng = result.lng()
var latLng = {
lat,
lng
}
console.log(latLng.lat+', '+latLng.lng );
}
});
if (location != '') {
var filtered = data.filter(function(item) {
//can't use latLng here
return getDistance(latLng.lat, latLng.lng, data.lat, data.lng) <= 10;
});
}
console.log(filtered.length);
I need to be able to use latLng.lat and latLng.lng in "filtered".