Jquery 谷歌地图,自动完成显示选定国家的州名
是否可以使用谷歌地图获取选定国家的自动完成状态的唯一列表 目前,我正在自动完成所选国家/地区的城市、街道、州 屏幕截图:Jquery 谷歌地图,自动完成显示选定国家的州名,jquery,google-maps,autocomplete,Jquery,Google Maps,Autocomplete,是否可以使用谷歌地图获取选定国家的自动完成状态的唯一列表 目前,我正在自动完成所选国家/地区的城市、街道、州 屏幕截图: var map, places, infoWindow; var markers = []; var autocomplete; var countryRestrict = { 'country': 'in' }; var MARKER_PATH = 'https://maps.gstatic.com/intl/en_us/mapfile
var map, places, infoWindow;
var markers = [];
var autocomplete;
var countryRestrict = { 'country': 'in' };
var MARKER_PATH = 'https://maps.gstatic.com/intl/en_us/mapfiles/marker_green';
var hostnameRegexp = new RegExp('^https?://.+?/');
var countries = {
'in': {
center: new google.maps.LatLng(21.76, 78.87),
zoom: 4
},
'au': {
center: new google.maps.LatLng(-25.3, 133.8),
zoom: 4
},
'br': {
center: new google.maps.LatLng(-14.2, -51.9),
zoom: 3
},
'ca': {
center: new google.maps.LatLng(62, -110.0),
zoom: 3
},
'fr': {
center: new google.maps.LatLng(46.2, 2.2),
zoom: 5
},
'de': {
center: new google.maps.LatLng(51.2, 10.4),
zoom: 5
},
'mx': {
center: new google.maps.LatLng(23.6, -102.5),
zoom: 4
},
'nz': {
center: new google.maps.LatLng(-40.9, 174.9),
zoom: 5
},
'it': {
center: new google.maps.LatLng(41.9, 12.6),
zoom: 5
},
'za': {
center: new google.maps.LatLng(-30.6, 22.9),
zoom: 5
},
'es': {
center: new google.maps.LatLng(40.5, -3.7),
zoom: 5
},
'pt': {
center: new google.maps.LatLng(39.4, -8.2),
zoom: 6
},
'us': {
center: new google.maps.LatLng(37.1, -95.7),
zoom: 3
},
'uk': {
center: new google.maps.LatLng(54.8, -4.6),
zoom: 5
}
};
function initialize() {
var myOptions = {
zoom: countries['us'].zoom,
center: countries['us'].center,
mapTypeControl: false,
panControl: false,
zoomControl: false,
streetViewControl: false
};
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
infoWindow = new google.maps.InfoWindow({
content: document.getElementById('info-content')
});
// Create the autocomplete object and associate it with the UI input control.
// Restrict the search to the default country, and to place type "cities".
autocomplete = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */(document.getElementById('autocomplete')),
{
//types: ['(cities)'],
// types: ['(regions)'],
types: ['(regions)'],
componentRestrictions: countryRestrict
});
places = new google.maps.places.PlacesService(map);
google.maps.event.addListener(autocomplete, 'place_changed', onPlaceChanged);
// Add a DOM event listener to react when the user selects a country.
google.maps.event.addDomListener(document.getElementById('country'), 'change',
setAutocompleteCountry);
}
// When the user selects a city, get the place details for the city and
// zoom the map in on the city.
function onPlaceChanged() {
var place = autocomplete.getPlace();
if (place.geometry) {
map.panTo(place.geometry.location);
map.setZoom(15);
search();
} else {
document.getElementById('autocomplete').placeholder = 'Enter a city';
}
}
// Search for hotels in the selected city, within the viewport of the map.
function search() {
var search = {
bounds: map.getBounds(),
types: ['lodging']
};
places.nearbySearch(search, function (results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
clearResults();
clearMarkers();
// Create a marker for each hotel found, and
// assign a letter of the alphabetic to each marker icon.
for (var i = 0; i < results.length; i++) {
var markerLetter = String.fromCharCode('A'.charCodeAt(0) + i);
var markerIcon = MARKER_PATH + markerLetter + '.png';
// Use marker animation to drop the icons incrementally on the map.
markers[i] = new google.maps.Marker({
position: results[i].geometry.location,
animation: google.maps.Animation.DROP,
icon: markerIcon
});
// If the user clicks a hotel marker, show the details of that hotel
// in an info window.
markers[i].placeResult = results[i];
google.maps.event.addListener(markers[i], 'click', showInfoWindow);
setTimeout(dropMarker(i), i * 100);
addResult(results[i], i);
}
}
});
}
function clearMarkers() {
for (var i = 0; i < markers.length; i++) {
if (markers[i]) {
markers[i].setMap(null);
}
}
markers = [];
}
// Set the country restriction based on user input.
// Also center and zoom the map on the given country.
function setAutocompleteCountry() {
var country = document.getElementById('country').value;
if (country == 'all') {
autocomplete.setComponentRestrictions([]);
map.setCenter(new google.maps.LatLng(15, 0));
map.setZoom(2);
} else {
autocomplete.setComponentRestrictions({ 'country': country });
map.setCenter(countries[country].center);
map.setZoom(countries[country].zoom);
}
clearResults();
clearMarkers();
}
function dropMarker(i) {
return function () {
markers[i].setMap(map);
};
}
function addResult(result, i) {
var results = document.getElementById('results');
var markerLetter = String.fromCharCode('A'.charCodeAt(0) + i);
var markerIcon = MARKER_PATH + markerLetter + '.png';
var tr = document.createElement('tr');
tr.style.backgroundColor = (i % 2 == 0 ? '#F0F0F0' : '#FFFFFF');
tr.onclick = function () {
google.maps.event.trigger(markers[i], 'click');
};
var iconTd = document.createElement('td');
var nameTd = document.createElement('td');
var icon = document.createElement('img');
icon.src = markerIcon;
icon.setAttribute('class', 'placeIcon');
icon.setAttribute('className', 'placeIcon');
var name = document.createTextNode(result.name);
iconTd.appendChild(icon);
nameTd.appendChild(name);
tr.appendChild(iconTd);
tr.appendChild(nameTd);
results.appendChild(tr);
}
function clearResults() {
var results = document.getElementById('results');
while (results.childNodes[0]) {
results.removeChild(results.childNodes[0]);
}
}
// Get the place details for a hotel. Show the information in an info window,
// anchored on the marker for the hotel that the user selected.
function showInfoWindow() {
var marker = this;
places.getDetails({ reference: marker.placeResult.reference },
function (place, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
return;
}
infoWindow.open(map, marker);
buildIWContent(place);
});
}
// Load the place information into the HTML elements used by the info window.
function buildIWContent(place) {
document.getElementById('iw-icon').innerHTML = '<img class="hotelIcon" ' +
'src="' + place.icon + '"/>';
document.getElementById('iw-url').innerHTML = '<b><a href="' + place.url +
'">' + place.name + '</a></b>';
document.getElementById('iw-address').textContent = place.vicinity;
if (place.formatted_phone_number) {
document.getElementById('iw-phone-row').style.display = '';
document.getElementById('iw-phone').textContent =
place.formatted_phone_number;
} else {
document.getElementById('iw-phone-row').style.display = 'none';
}
// Assign a five-star rating to the hotel, using a black star ('✭')
// to indicate the rating the hotel has earned, and a white star ('✩')
// for the rating points not achieved.
if (place.rating) {
var ratingHtml = '';
for (var i = 0; i < 5; i++) {
if (place.rating < (i + 0.5)) {
ratingHtml += '✩';
} else {
ratingHtml += '✭';
}
document.getElementById('iw-rating-row').style.display = '';
document.getElementById('iw-rating').innerHTML = ratingHtml;
}
} else {
document.getElementById('iw-rating-row').style.display = 'none';
}
// The regexp isolates the first part of the URL (domain plus subdomain)
// to give a short URL for displaying in the info window.
if (place.website) {
var fullUrl = place.website;
var website = hostnameRegexp.exec(place.website);
if (website == null) {
website = 'http://' + place.website + '/';
fullUrl = website;
}
document.getElementById('iw-website-row').style.display = '';
document.getElementById('iw-website').textContent = website;
} else {
document.getElementById('iw-website-row').style.display = 'none';
}
}
代码:
var map, places, infoWindow;
var markers = [];
var autocomplete;
var countryRestrict = { 'country': 'in' };
var MARKER_PATH = 'https://maps.gstatic.com/intl/en_us/mapfiles/marker_green';
var hostnameRegexp = new RegExp('^https?://.+?/');
var countries = {
'in': {
center: new google.maps.LatLng(21.76, 78.87),
zoom: 4
},
'au': {
center: new google.maps.LatLng(-25.3, 133.8),
zoom: 4
},
'br': {
center: new google.maps.LatLng(-14.2, -51.9),
zoom: 3
},
'ca': {
center: new google.maps.LatLng(62, -110.0),
zoom: 3
},
'fr': {
center: new google.maps.LatLng(46.2, 2.2),
zoom: 5
},
'de': {
center: new google.maps.LatLng(51.2, 10.4),
zoom: 5
},
'mx': {
center: new google.maps.LatLng(23.6, -102.5),
zoom: 4
},
'nz': {
center: new google.maps.LatLng(-40.9, 174.9),
zoom: 5
},
'it': {
center: new google.maps.LatLng(41.9, 12.6),
zoom: 5
},
'za': {
center: new google.maps.LatLng(-30.6, 22.9),
zoom: 5
},
'es': {
center: new google.maps.LatLng(40.5, -3.7),
zoom: 5
},
'pt': {
center: new google.maps.LatLng(39.4, -8.2),
zoom: 6
},
'us': {
center: new google.maps.LatLng(37.1, -95.7),
zoom: 3
},
'uk': {
center: new google.maps.LatLng(54.8, -4.6),
zoom: 5
}
};
function initialize() {
var myOptions = {
zoom: countries['us'].zoom,
center: countries['us'].center,
mapTypeControl: false,
panControl: false,
zoomControl: false,
streetViewControl: false
};
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
infoWindow = new google.maps.InfoWindow({
content: document.getElementById('info-content')
});
// Create the autocomplete object and associate it with the UI input control.
// Restrict the search to the default country, and to place type "cities".
autocomplete = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */(document.getElementById('autocomplete')),
{
//types: ['(cities)'],
// types: ['(regions)'],
types: ['(regions)'],
componentRestrictions: countryRestrict
});
places = new google.maps.places.PlacesService(map);
google.maps.event.addListener(autocomplete, 'place_changed', onPlaceChanged);
// Add a DOM event listener to react when the user selects a country.
google.maps.event.addDomListener(document.getElementById('country'), 'change',
setAutocompleteCountry);
}
// When the user selects a city, get the place details for the city and
// zoom the map in on the city.
function onPlaceChanged() {
var place = autocomplete.getPlace();
if (place.geometry) {
map.panTo(place.geometry.location);
map.setZoom(15);
search();
} else {
document.getElementById('autocomplete').placeholder = 'Enter a city';
}
}
// Search for hotels in the selected city, within the viewport of the map.
function search() {
var search = {
bounds: map.getBounds(),
types: ['lodging']
};
places.nearbySearch(search, function (results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
clearResults();
clearMarkers();
// Create a marker for each hotel found, and
// assign a letter of the alphabetic to each marker icon.
for (var i = 0; i < results.length; i++) {
var markerLetter = String.fromCharCode('A'.charCodeAt(0) + i);
var markerIcon = MARKER_PATH + markerLetter + '.png';
// Use marker animation to drop the icons incrementally on the map.
markers[i] = new google.maps.Marker({
position: results[i].geometry.location,
animation: google.maps.Animation.DROP,
icon: markerIcon
});
// If the user clicks a hotel marker, show the details of that hotel
// in an info window.
markers[i].placeResult = results[i];
google.maps.event.addListener(markers[i], 'click', showInfoWindow);
setTimeout(dropMarker(i), i * 100);
addResult(results[i], i);
}
}
});
}
function clearMarkers() {
for (var i = 0; i < markers.length; i++) {
if (markers[i]) {
markers[i].setMap(null);
}
}
markers = [];
}
// Set the country restriction based on user input.
// Also center and zoom the map on the given country.
function setAutocompleteCountry() {
var country = document.getElementById('country').value;
if (country == 'all') {
autocomplete.setComponentRestrictions([]);
map.setCenter(new google.maps.LatLng(15, 0));
map.setZoom(2);
} else {
autocomplete.setComponentRestrictions({ 'country': country });
map.setCenter(countries[country].center);
map.setZoom(countries[country].zoom);
}
clearResults();
clearMarkers();
}
function dropMarker(i) {
return function () {
markers[i].setMap(map);
};
}
function addResult(result, i) {
var results = document.getElementById('results');
var markerLetter = String.fromCharCode('A'.charCodeAt(0) + i);
var markerIcon = MARKER_PATH + markerLetter + '.png';
var tr = document.createElement('tr');
tr.style.backgroundColor = (i % 2 == 0 ? '#F0F0F0' : '#FFFFFF');
tr.onclick = function () {
google.maps.event.trigger(markers[i], 'click');
};
var iconTd = document.createElement('td');
var nameTd = document.createElement('td');
var icon = document.createElement('img');
icon.src = markerIcon;
icon.setAttribute('class', 'placeIcon');
icon.setAttribute('className', 'placeIcon');
var name = document.createTextNode(result.name);
iconTd.appendChild(icon);
nameTd.appendChild(name);
tr.appendChild(iconTd);
tr.appendChild(nameTd);
results.appendChild(tr);
}
function clearResults() {
var results = document.getElementById('results');
while (results.childNodes[0]) {
results.removeChild(results.childNodes[0]);
}
}
// Get the place details for a hotel. Show the information in an info window,
// anchored on the marker for the hotel that the user selected.
function showInfoWindow() {
var marker = this;
places.getDetails({ reference: marker.placeResult.reference },
function (place, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
return;
}
infoWindow.open(map, marker);
buildIWContent(place);
});
}
// Load the place information into the HTML elements used by the info window.
function buildIWContent(place) {
document.getElementById('iw-icon').innerHTML = '<img class="hotelIcon" ' +
'src="' + place.icon + '"/>';
document.getElementById('iw-url').innerHTML = '<b><a href="' + place.url +
'">' + place.name + '</a></b>';
document.getElementById('iw-address').textContent = place.vicinity;
if (place.formatted_phone_number) {
document.getElementById('iw-phone-row').style.display = '';
document.getElementById('iw-phone').textContent =
place.formatted_phone_number;
} else {
document.getElementById('iw-phone-row').style.display = 'none';
}
// Assign a five-star rating to the hotel, using a black star ('✭')
// to indicate the rating the hotel has earned, and a white star ('✩')
// for the rating points not achieved.
if (place.rating) {
var ratingHtml = '';
for (var i = 0; i < 5; i++) {
if (place.rating < (i + 0.5)) {
ratingHtml += '✩';
} else {
ratingHtml += '✭';
}
document.getElementById('iw-rating-row').style.display = '';
document.getElementById('iw-rating').innerHTML = ratingHtml;
}
} else {
document.getElementById('iw-rating-row').style.display = 'none';
}
// The regexp isolates the first part of the URL (domain plus subdomain)
// to give a short URL for displaying in the info window.
if (place.website) {
var fullUrl = place.website;
var website = hostnameRegexp.exec(place.website);
if (website == null) {
website = 'http://' + place.website + '/';
fullUrl = website;
}
document.getElementById('iw-website-row').style.display = '';
document.getElementById('iw-website').textContent = website;
} else {
document.getElementById('iw-website-row').style.display = 'none';
}
}
var地图、地点、信息窗口;
var标记=[];
var自动完成;
var countryRestrict={'country':'in'};
变量标记器https://maps.gstatic.com/intl/en_us/mapfiles/marker_green';
var hostnameRegexp=new RegExp(“^https:/。+?/”);
var国家={
'在':{
中心:新google.maps.LatLng(21.76,78.87),
缩放:4
},
"au":{
中心:新google.maps.LatLng(-25.3133.8),
缩放:4
},
“br”:{
中心:新google.maps.LatLng(-14.2,-51.9),
缩放:3
},
“ca”:{
中心:新google.maps.LatLng(62,-110.0),
缩放:3
},
“fr”:{
中心:新google.maps.LatLng(46.2,2.2),
缩放:5
},
“德”:{
中心:新google.maps.LatLng(51.2,10.4),
缩放:5
},
“mx”:{
中心:新google.maps.LatLng(23.6,-102.5),
缩放:4
},
“新西兰”:{
中心:新google.maps.LatLng(-40.9174.9),
缩放:5
},
“它”:{
中心:新google.maps.LatLng(41.9,12.6),
缩放:5
},
"za":{
中心:新google.maps.LatLng(-30.6,22.9),
缩放:5
},
“es”:{
中心:新google.maps.LatLng(40.5,-3.7),
缩放:5
},
“pt”:{
中心:新google.maps.LatLng(39.4,-8.2),
缩放:6
},
“我们”{
中心:新google.maps.LatLng(37.1,-95.7),
缩放:3
},
“英国”:{
中心:新google.maps.LatLng(54.8,-4.6),
缩放:5
}
};
函数初始化(){
变量myOptions={
zoom:countries['us']。zoom,
中心:国家[美国]。中心,
mapTypeControl:false,
泛控制:错误,
动物控制:错误,
街景控制:错误
};
map=new google.maps.map(document.getElementById('map-canvas'),myOptions);
infoWindow=新建google.maps.infoWindow({
内容:document.getElementById('info-content')
});
//创建自动完成对象并将其与UI输入控件关联。
//将搜索限制为默认国家/地区,并放置类型“cities”。
autocomplete=new google.maps.places.autocomplete(
/**@type{HTMLInputElement}*/(document.getElementById('autocomplete'),
{
//类型:['(城市)],
//类型:['(区域)],
类型:['(区域)],
组件限制:countryRestrict
});
places=新的google.maps.places.PlacesService(地图);
google.maps.event.addListener(自动完成,'place\u changed',onPlaceChanged);
//添加DOM事件侦听器,以便在用户选择国家/地区时作出反应。
google.maps.event.addDomListener(document.getElementById('country'),'change',
设置自动完成国家/地区);
}
//当用户选择一个城市时,获取该城市的地点详细信息,然后
//放大这座城市的地图。
函数onPlaceChanged(){
var place=autocomplete.getPlace();
if(place.geometry){
panTo地图(地点、几何、位置);
map.setZoom(15);
搜索();
}否则{
document.getElementById('autocomplete')。占位符='输入城市';
}
}
//在地图的视口中搜索选定城市中的酒店。
函数搜索(){
变量搜索={
边界:map.getBounds(),
类型:[“住宿”]
};
places.nearbySearch(搜索、功能(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
clearResults();
clearMarkers();
//为找到的每个酒店创建一个标记,然后
//为每个标记图标指定一个字母。
对于(var i=0;i