2

Cookie を設定しようとしており、それを読み取って Google ジオコーディング スクリプトに挿入しようとしていますが、うまくいかないようです。自動提案の結果は米国のものだけですが、例ではすべての国から提案されていることがわかります。

これは私が取り組んでいるコードです:

http://jsfiddle.net/sR4GR/4/

これは生のコードです:

<script type="text/javascript" src="//maps.google.com/maps/api/js?sensor=true&libraries=places"></script>
<script type="text/javascript" src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>

<script type="text/javascript">

    // SET COOKIE FOR TESTING   
$.cookie("country", "us");

    // GEOCODE RESULT
 function geocode(){
    var GeoCoded = { done: false };
    var input = document.getElementById('loc');
    var options = { types: []};
    var country_code = $.cookie('country');
    if (country_code) { options.componentRestrictions= { 'country': country_code }; }
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    $('#searchform').on('submit',function(e){
       if(GeoCoded.done)
            return true;
        e.preventDefault();
        var geocoder = new google.maps.Geocoder();
        var address = document.getElementById('loc').value;

        $('#searchform input[type="submit"]').attr('disabled',true);

        geocoder.geocode({
            'address': address
        },
        function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                $('#lat').val(results[0].geometry.location.lat());
                $('#lng').val(results[0].geometry.location.lng());
                GeoCoded.done = true;
                $('#searchform').submit();
            } else {
                $('#searchform input[type="submit"]').attr('disabled',false);
                alert("Damn! We couldn't find this location")
            }

        });

    });

};      
</script>

<body onload="geocode()">
<form name="searchform">
        <input class="kw" id="keyword" placeholder="Keyword"></input>
        <input id="loc" placeholder="Location" type="text"></input>
        <input type="submit" value="Search" id="search">
        <input class="hidden" id="lat" disabled="true" placeholder="lat"></input>
        <input class="hidden" id="lng" disabled="true" placeholder="lng"></input>
</form>
4

1 に答える 1

1

上記のコードは正しく記述され、実行されます。Cookie は Chrome と Firefox に設定されます。Safari では、デフォルトの Cookie 設定は「From Visited」で、比較的厳密な設定です。これらの Cookie を設定できるようにするには、ユーザーは Safari の設定を変更して Cookie を「常に」許可する必要があります。

これに対する上品な回避策はないようですが、考えられる解決策の 1 つは、Cookie が返されるかどうかを確認し、返さundefinedれた場合は、ユーザーに「設定に移動して Cookie を有効にしてください。あなたのセキュリティ意識の高いデバイス.うーん? ありがとう."

このスレッドには、Safari と Cookie に関する詳細情報が含まれています

于 2013-05-14T12:11:48.547 に答える