有没有人有想法如何在Google地图pnearby地方搜索中传递多种类型的types
var map;
var infowindow;
function initMap() {
var pyrmont = {lat: -33.867, lng: 151.195};
map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: pyrmont,
radius: 500,
types: ['store']
}, callback);
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
我尝试用多种语法编写类型,如下所示,但显示错误的地方。
types: 'bank|bar|airport' types: ['bank','bar'] types: ['bank'|'bar']
任何人都有更好的解决方案我会很感激。
答案 0 :(得分:0)
您缺少必需的属性query
。这为要搜索的API提供了一个关键字,就像商家名称一样。这些类型提供了额外的过滤器,以提供它认为与您的请求最相关的信息。
此方法接受带有以下字段的请求:
var map;
var infowindow;
function initMap() {
var timesSquare = {
lat: 40.759011,
lng: -73.984472
};
map = new google.maps.Map(document.getElementById('map'), {
center: timesSquare,
zoom: 18
});
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: timesSquare,
radius: 50,
query: 'new york',
types: ['bank'|'bar']
}, callback);
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
console.log(results);
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
#map {
min-width: 100%;
height: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3&callback=initMap&libraries=places" async defer>
</script>
<div id="map"></div>