首页 > 解决方案 > 从 google api 函数返回 lat 和 lng

问题描述

一段时间以来,我一直试图让 lat 和 lng 脱离这个函数,但我真的不知道为什么我不能返回它。它 console.logs 它在函数中就好了。

有人可以帮我提供一个解决方案来返回 lat 和 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>

标签: javascript

解决方案


推荐阅读