在尝试从Google Places API中提取城市时,为什么我的自动填充搜索功能会冻结?

时间:2016-05-16 18:43:18

标签: javascript jquery autocomplete google-places-api

在我的网页基本模板中,我有JQuery链接,Google Places API,自动完成插件和JS代码来调用功能。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDaa7NZzS-SE4JW3J-7TaA1v1Y5aWUTiyc&libraries=places"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/geocomplete/1.7.0/jquery.geocomplete.js"></script>

 <script>

    $(function(){

    $("#searchBoxGlow").geocomplete()

    });

    </script>

最后,我有一个搜索字段,我想自动填写城市名称

<input type="text" class="form-control input-lg" id="searchBoxGlow" name="city" placeholder="Search Cities">

然而,它冻结并在此屏幕截图中显示感叹号:enter image description here

1 个答案:

答案 0 :(得分:3)

这对我有用。

<input type="text" id="searchplace" />
<input type="hidden" id="latitude" name="latitude"/>
<input type="hidden" id="longitude" name="longitude"/>

在那个html部分中,我添加了这个:

<script type="text/javascript">
    function initialize() {
        var searchBox = document.getElementById('searchplace');
        var autocomplete =  new google.maps.places.Autocomplete(searchBox);
        var latitude;
        var longitude;

        google.maps.event.addListener(autocomplete, 'place_changed', function () {
            var place = autocomplete.getPlace();

            latitude = place.geometry.location.lat();
            longitude = place.geometry.location.lng();
            document.getElementById('latitude').value = place.geometry.location.lat();
            document.getElementById('longitude').value = place.geometry.location.lng();                                     
        });                                             
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>