我应该在Google Api javascript中进行什么更改,以便它只能显示印度城市,而不能落后于州和印度
What change should i make in Google Api javascript so it can only show the indian cities without trailing state and india?
我应该在Google Api javascript中进行哪些更改,以便它只能显示印度城市,而不能落后于州和印度?
<html>
<head>
<script>
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
//autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */ //(document.getElementById('autocomplete')),
// {types: ['geocode']});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
var autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'), {
types: ['(cities)']
});
autocomplete.addListener('place_changed', fillInAddress);
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC1YjmCfbQrueEjKUbHAjzOZf1OpeQKqOU&libraries=places&callback=initAutocomplete" async defer></script>
</head>
<body>
<input id="autocomplete" placeholder="Enter The City " onFocus="geolocate()" name="ct" type="text" required>
</body>
</html>
使用 AutocompleteService() 获取预测,您可以绑定自定义 droupdown。
<!DOCTYPE html>
<html>
<head>
<title>Retrieving Autocomplete Predictions</title>
</head>
<body>
<div id="right-panel">
<p>Query suggestions for city:</p>
<ul id="results"></ul>
</div>
<script>
function initService() {
var displaySuggestions = function (predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
var a = predictions;
predictions.forEach(function (prediction) {
var li = document.createElement('li');
//li.appendChild(document.createTextNode(prediction.description));
li.appendChild(document.createTextNode(prediction.terms[0].value));
document.getElementById('results').appendChild(li);
});
};
var service = new google.maps.places.AutocompleteService();
service.getPlacePredictions(
{
input: "jai",
types: ['(cities)'],
componentRestrictions: {country: 'in'}
},
displaySuggestions);
<!-- service.getQueryPredictions({ input: 'j' }, displaySuggestions); -->
}
</script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC1YjmCfbQrueEjKUbHAjzOZf1OpeQKqOU&libraries=places&callback=initService"
async defer></script>
</body>
</html>
试试,这可能会有所帮助
<!DOCTYPE html>
<html>
<head>
<title>Retrieving Autocomplete Predictions</title>
<style>
.dropdown ul{
position:absolute;
border:1px solid #ddd;
display:black;
margin:0;
padding:0;
}
.dropdown li{
list-style:none;
padding:3px 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="right-panel">
<p>Query suggestions for city:</p>
<ul id="results"></ul>
</div>
<script>
function initService() {
displaySuggestions = function (predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
var options = '<ul>';
predictions.forEach(function (prediction) { options += ('<li onclick="clicked(this)">' + prediction.terms[0].value + '</li>');
});
options += '</ul>';
document.getElementById('ddlCity').innerHTML = options;
document.getElementById('ddlCity').style.display = 'block';
};
}
function GetCity(){
var keywords = document.getElementById('autocomplete').value;
if(keywords != ''){
var service = new google.maps.places.AutocompleteService();
service.getPlacePredictions(
{
input: keywords,
types: ['(cities)'],
componentRestrictions: {country: 'in'}
},
displaySuggestions);
}
else
{
document.getElementById('ddlCity').style.display = 'none';
}
}
function clicked(obj){
document.getElementById('autocomplete').value = obj.innerHTML;
document.getElementById('ddlCity').style.display = 'none';
}
</script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC1YjmCfbQrueEjKUbHAjzOZf1OpeQKqOU&libraries=places&callback=initService"
async defer></script>
<input id="autocomplete" placeholder="Enter The City " name="ct" onkeyup="GetCity()
;" type="text" required>
<div id="ddlCity" class="dropdown">
</div>
</body>
</html>
相关文章:
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何在生成下载文件时显示加载动画
- React重新渲染但未显示正确的组件
- 我应该在Google Api javascript中进行什么更改,以便它只能显示印度城市,而不能落后于州和印度
- 一次高亮显示一个城市,并在悬停其他城市时删除颜色填充
- 谷歌地图的天气图层未针对不同的城市显示
- 如何根据同一城市内的地理位置显示独特的内容
- 谷歌/必应地图显示所有城市
- 显示Maxmind Geoip2显示国家,城市,地区和ISP与Javascript
- 将访客所在城市与GEOIP匹配时显示文本
- 赢得't在地图上显示城市和类似的地方
- 如何显示城市的谷歌地图
- 如何在Angularjs中显示国家、州和城市依赖
- 如何使用jVectorMap插件在世界地图上突出显示某个城市
- 如何在地图上显示多个城市使用谷歌地图API与纬度和经度和值
- 显示不同的时区为不同的城市,国家
- 使用topojson和d3js在澳大利亚地图上显示城市
- 用D3和TopoJson显示地点(城市)数据
- 如何显示带有街道,城市和国家/地区的手机的当前位置