我正在尝试过滤搜索框,土耳其只有3个区“Sarıyer”,“Beşiktaş”,“Şişli”。我在address_components中为long_name做了条件,但它无法正常工作。
我认为通过写这个条件,如果用户搜索这三个区域标记将在地图上设置。或者,如果用户搜索其他地方,则会显示弹出消息“您只能在这3个区域中搜索”
如何过滤搜索框
var addCom1 = place.address_components[0].long_name;
var addCom2 = place.address_components[1].long_name;
var addCom3 = place.address_components[2].long_name;
var addCom4 = place.address_components[3].long_name;
var addCom5 = place.address_components[4].long_name;
var addCom6 = place.address_components[5].long_name;
if(addCom1 == "Şişli" || addCom1 == "Beşiktaş" || addCom1 == "Sarıyer" ||
addCom2 == "Şişli" || addCom2 == "Beşiktaş" || addCom2 == "Sarıyer" ||
addCom3 == "Şişli" || addCom3 == "Beşiktaş" || addCom3 == "Sarıyer" ||
addCom4 == "Şişli" || addCom4 == "Beşiktaş" || addCom4 == "Sarıyer" ||
addCom5 == "Şişli" || addCom5 == "Beşiktaş" || addCom5 == "Sarıyer" ||
addCom6 == "Şişli" || addCom6 == "Beşiktaş" || addCom6 == "Sarıyer"){
// Here i need set the marker
}else{
// Pop up messsage "You can only search these 3 districts"
}
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 41.1810741, lng: 29.0094592},
zoom: 10,
mapTypeId: 'roadmap
});
options = {
language: 'tr',
types: ['(cities)'],
componentRestrictions: { country: "tr" }
};
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input, options);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
var addCom1 = place.address_components[0].long_name;
var addCom2 = place.address_components[1].long_name;
var addCom3 = place.address_components[2].long_name;
var addCom4 = place.address_components[3].long_name;
var addCom5 = place.address_components[4].long_name;
var addCom6 = place.address_components[5].long_name;
if(addCom1 == "Şişli" || addCom1 == "Beşiktaş" || addCom1 == "Sarıyer" ||
addCom2 == "Şişli" || addCom2 == "Beşiktaş" || addCom2 == "Sarıyer" ||
addCom3 == "Şişli" || addCom3 == "Beşiktaş" || addCom3 == "Sarıyer" ||
addCom4 == "Şişli" || addCom4 == "Beşiktaş" || addCom4 == "Sarıyer" ||
addCom5 == "Şişli" || addCom5 == "Beşiktaş" || addCom5 == "Sarıyer" ||
addCom6 == "Şişli" || addCom6 == "Beşiktaş" || addCom6 == "Sarıyer"){
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
}
// console.log(place);
$(".pin-address").text(place.formatted_address);
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});