3

Google マップ ジオコード API に対してクロス ドメイン呼び出しを行っています。これは、最新のブラウザーでは問題なく機能していましたが、IE8 ではまったく機能していませんでした。IE9でも失敗するようです(部分的なCORSサポート)。これにより、IE8-9 を処理する XDomainRequest (XDR) が含まれるようになりました。これを行うと、IE8 でデータを取得するためのスタンドアロン テストで問題なく動作しました。

私が現在直面している問題は、XDR が非同期でのみ動作するため、xdr.onload が起動する前にジオコード関数が返されることです。

私の検索関数では、ジオコード関数を呼び出します。

var location = Geocode(city, state);

if (!location) {
    alert('Unable to determine the location of the city and state you entered');
    StopLoading();
    return;
}
//function then uses location.lat and location.lng coordinates

上記の IE8 で「場所を特定できません」というアラートが表示されます。

ここに私のジオコード関数があります:

Geocode = function (address, state) {
var protocol = location.protocol,
    url = '//maps.googleapis.com/maps/api/geocode/json?sensor=false&address=',
    param = encodeURIComponent(address + ', ' + state),
    json = {};

if ('XDomainRequest' in window && window.XDomainRequest !== null) {
    //IEs that do not support cross domain xhr requests
    var xdr = new XDomainRequest();

    xdr.open('get', protocol + url + param);
    xdr.onload = function() {
        json = jQuery.parseJSON(xdr.responseText);
    };
    xdr.send();
} else {
    //good browsers
    jQuery.ajax({
        url: protocol + url + param,
        type: 'get',
        dataType: 'json',
        async: false,
        success: function(data) {
            json = data;
        }
    });
}

//alert(json);
if (json.status !== 'OK') {
    alert('Unable to determine the location of the city and state you entered');
    return null;
}

return json.results[0].geometry.location;
};

ジオコード関数でアラート (json) をコメント アウトすると、IE8 で結果が得られます。これはブロック操作であるため、リクエストが終了して json オブジェクトにデータが入力される時間があります。コメントを外して実行すると、json オブジェクトは設定されません。

IEでこれを機能させる方法を知っている人はいますか?

4

1 に答える 1