Google maps api 3 使用Google Places API在自动完成建议中仅获取城市名称
我使用下面的代码来获得城市建议,但它给了我城市+州+国家,但我只需要城市名称Google maps api 3 使用Google Places API在自动完成建议中仅获取城市名称,google-maps-api-3,autocomplete,google-places-api,Google Maps Api 3,Autocomplete,Google Places Api,我使用下面的代码来获得城市建议,但它给了我城市+州+国家,但我只需要城市名称 <html> <head> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script> <script> var autocomplete; func
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<script>
var autocomplete;
function initialize() {
autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'), { types: ['(cities)'], componentRestrictions : { country: 'in' }});
}
</script>
</head>
<body onload="initialize()">
<div id="locationField">
<input id="autocomplete" placeholder="Enter your address" onFocus="geolocate()" type="text"></input>
</div>
</body>
</html>
var自动完成;
函数初始化(){
autocomplete=new google.maps.places.autocomplete(document.getElementById('autocomplete'),{types:['(cities)”,componentRestrictions:{country:'in'});
}
有什么建议吗 您不能删除这些信息,但是可以使用CSS屏蔽它们
请参阅
引用官方文档,这里是自动完成
组件使用的类。
因此,我建议您使用CSS隐藏您不想显示的部分。在您的情况下,您可以使用:
.pac-item-query + span
{
display: none
}
这将隐藏
到
的下一个
,其类为pac项目查询
(仅包含建议的城市名称),因此,仅显示城市。因此,根据您的要求,州/国家将被屏蔽
小心,并非所有浏览器都支持
+
伪选择器,包括IE6。使用前请检查您的浏览器要求。我的答案解决了您的问题吗?谢谢。但是这不是一个好的解决方案,所以我刚刚投票给了你的答案,你还需要什么,如果这对你来说不是一个好的解决方案,阿米特?我不想使用CSS,实际上谷歌可能会随时改变它…谢谢