尝试从Google Places API中提取城市时,为什么我的自动填充搜索字段会冻结? [英] Why does my autocomplete search fiels freeze when trying to pull cities from Google Places API?
本文介绍了尝试从Google Places API中提取城市时,为什么我的自动填充搜索字段会冻结?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的网页基本模板中,我具有用于JQuery,Google Places API,用于自动完成功能的插件和用于调用该功能的JS代码的链接.
In my web page base template, I have links for JQuery, Google Places API, plugin for autocomplete and JS code to to call the functionality.
<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>
最后,我有一个要自动填写城市名称的搜索字段
Finally I have a search field that I want to autocomplete city names
<input type="text" class="form-control input-lg" id="searchBoxGlow" name="city" placeholder="Search Cities">
但是它会冻结并显示一个感叹号,如以下屏幕截图所示:
However it freezes and shows an exclamation mark as in this screenshot:
推荐答案
这对我有用.
<input type="text" id="searchplace" />
<input type="hidden" id="latitude" name="latitude"/>
<input type="hidden" id="longitude" name="longitude"/>
在该html部分中,我添加了以下内容:
and within that html part, I added this:
<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>
这篇关于尝试从Google Places API中提取城市时,为什么我的自动填充搜索字段会冻结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文