我已经成功集成了 Geocomplete jquery plugin在 wordpress 网站上。我的问题是我不能将 self 暗示结果限制在邻里或次区域。我彻底修改了 Google Place 类型,但我的结果总是一样。我知道我在某处做错了什么。这是我到目前为止所拥有的:
add_action( 'wp_head', 'add_geocomplete_script' );
function add_geocomplete_script() {
?>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCJ1Bgyx1jSN3aRFB3uUkLLntXCONZfOyM&libraries=places"></script>
<script src="<?php echo get_stylesheet_directory_uri() ?>/js/jquery.geocomplete.js"></script>
<script src="<?php echo get_stylesheet_directory_uri() ?>/js/logger.js"></script>
<?php
}
function woocommerce_checkout_autocomplete(){
?>
<script>
jQuery(document).ready(function() {
var options = {
//types: ['(cities)'],
//types: ['sublocality'],
//componentTypes: ['neighborhood'],
types: ["geocode", "sublocality"],
country: 'za'
};
jQuery("#billing_myfield8").geocomplete(options);
jQuery("#billing_myfield8").on("keyup", function() {
//jQuery("#billing_myfield8").trigger("geocode");
console.log('You typed in the input box');
});
});
</script>
<?php
}
add_action( 'wp_footer', 'woocommerce_checkout_autocomplete' );
使用上面的代码,我仍然可以搜索地址,例如。无处街112号。它不会过滤结果。
最佳答案
首先,请注意Place Autocomplete中的弃用通知。其中指出
The
types
parameter is deprecated as of February 16, 2016, replaced by a newtype
parameter which only supports one type per search request.
因此,我建议您使用 type
参数将结果限制为匹配指定类型的地方。只能指定一种类型(如果提供了多种类型,则忽略第一个条目之后的所有类型)。
此外,来自 list of supported types对于地点自动完成请求,您可以使用 regions
类型返回任何匹配 sublocality
的结果。
最后,如果你通过 Place Searches documentation 会有所帮助并检查此 SO post 中给出的解决方案.
关于javascript - Google Places Autocomplete 限制在附近,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40058499/