在Googlemaps API Geocoder中改变JS状态变量不工作

Change of JS State Variable in Googlemaps API Geocoder does not work

本文关键字:状态 变量 工作 JS 改变 Googlemaps API Geocoder      更新时间:2023-09-26

大家好,比我聪明。;)我真的弄不明白我的代码的这种奇怪行为。

它应该做的是在按下按钮时执行函数fixedlocation(),以检查是否可以找到有效的地址,然后执行一些代码。

为了检查它是否正常工作,我在代码中有一个警报,它给了我应该改变的布尔值的状态。现在我真的不明白为什么,但它只工作从第二次按下按钮向前。这意味着,即使可以找到一个有效的地址,第一次警报将弹出一个false…

起初我认为它必须与我定义变量的地方有关,但它不能第二次工作,可以吗?

任何帮助或指出正确的方向将不胜感激。

var geoinfobool=new Boolean();
function fixedlocation()
{    
    var addresscoordinates = new google.maps.LatLng(document.getElementById('addresslat').value,document.getElementById('addresslng').value)
    geocoder.geocode({'latLng': addresscoordinates}, function(results, status) 
    {
        if (status == google.maps.GeocoderStatus.OK) 
        {
            geoinfoavailable(true);
            //do other stuff in here//
        } 
        else 
        {
            geoinfoavailable(false);
            //do other stuff in here//
        }
    });
    alert(geoinfobool);
}
function geoinfoavailable(state)
{
    geoinfobool = state;
}

您的代码中没有定义地理编码器。

var addresscoordinates = new google.maps.LatLng(document.getElementById('addresslat').value,document.getElementById('addresslng').value);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': addresscoordinates}, function(results, status) 
{