1

Ok, Here is my JS Function :

function GetLatLong() {
    var geocoder = new google.maps.Geocoder();
    var address = $('#txtAddress').val() + ', ' + $('#txtCity').val() + ', ' + $find('drpState').get_text() + ', ' + $find('drpCountry').get_text();
    var result = false;
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var location = results[0].geometry.location;
            $('#hiddenLatLong').val(location);
            result = true;
        }
        else {
            alert("Geocode was not successful for the following reason: " + status);
            result = true;
        }
    });

    return result;
}

Now, what i want is, i want to return the result once i store the value in hidden field. Here what happens is, i am calling this function on button click, but as the call is asynchronous, it returns false on the click and i cannot get the resultant value in hidden field. Is there any way where i can wait or some work around with which it can be obtained in the hidden field?

4

1 に答える 1

1

ジオコード関数の 2 番目のパラメーターは、ジオコードの結果が返された場合に呼び出されるコールバック関数です。したがって、Google が結果を返す場合は、指定されたコードから非表示の値を設定する必要があります。

ただし、GetLatLong()関数は以前に完了しているため、 が返されますfalse。したがって、このメソッドの戻り値に依存する処理を行うべきではありません。

于 2012-05-24T06:30:19.000 に答える