0

I have been trying to get the lat and lng out of this function for a while but I am really at a loss as to why I can't return it. It console.logs it within the function just fine.

Can some help me to provide a solution to return the lat and lng?

<script>

    
    function latLngFind(postcode){
        var location = postcode + ' United Kingdom'
        axios.get('https://maps.googleapis.com/maps/api/geocode/json',{
            params:{
                address:location,
                key:'my key'
            }
        })
        .then(function(responce){
            lat = (responce.data.results[0].geometry.location.lat);
            lng = (responce.data.results[0].geometry.location.lng);
            console.log([lat,lng]);
            return[lat,lng];
        })
        .catch(function(error){
            console.log(error);
        });
    }
    postcode = 'S404UH'
    console.log(latLngFind(postcode));
</script>
  • Use aysnc/await function or use like latLngFind(postcode).then(latlongs => {}); – Manish Jangir Jul 28 '21 at 10:18
  • You need to return the Promise: `return axios.get(...)`, and then use `.then()` (or await) on the returned promise to grab the returned value: `latLngFind(postcode).then(latlng => console.log(latlng));` – Nick Parsons Jul 28 '21 at 10:18

0 Answers0