簡體   English   中英

Google地理編碼 - 坐標未返回輸入框

[英]Google Geocoding - Coordinates not being returned into input box

我一直在玩代碼,我有一些幾乎可以工作的東西,但坐標實際上並沒有移動到我的輸入框。 我已經重新檢查了幾次代碼,控制台沒有顯示錯誤,所以我有點卡住了。

這是我的代碼運行: 小提琴

這是我的實際代碼:

<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", "UK");

    // GEOCODE RESULT
 function geocode(){

    var GeoCoded = { done: false };
    var input = document.getElementById('loc');
    var options = { types: ['geocode']};
    var country_code = $.cookie('country');
    alert(country_code);
    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;
                alert("Geocoded!");
                $('#searchform').submit();
            } else {
                $('#searchform input[type="submit"]').attr('disabled',false);
                alert("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>

如果有人能指出我正確的方向,我將非常感激

  1. 選擇器#searchform與您的表單不匹配,表單沒有設置id屬性。

    使用選擇器form[name="searchform"]或將表單的id設置為searchform

  2. githhub不是CDN的jquery.cookie.js供應與內容類型"text/plain" ,因此(在某些瀏覽器)將被忽略,是什么導致錯誤,因為不確定的$.cookie

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM