我正在使用 Google Places API并使用 radarSearch 列出机构。但是,当我使用名称参数进行搜索时遇到问题。
它返回了几个地方,但我想得到确切的名字。
在此example ,我想返回“best buy”,但它返回的是与我正在搜索的词无关的地方。
var map;
var infoWindow;
var service;
function initialize() {
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(33.7490, -84.3880),
zoom: 11,
styles: [
{
stylers: [
{ visibility: 'simplified' }
]
},
{
elementType: 'labels',
stylers: [
{ visibility: 'on' }
]
}
]
});
infoWindow = new google.maps.InfoWindow();
service = new google.maps.places.PlacesService(map);
performSearch();
}
function performSearch() {
var request = {
location: new google.maps.LatLng(33.7490, -84.3880),
radius: 50000,
name: "Best Buy"
};
service.radarSearch(request, callback);
}
function callback(results, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
for (var i = 0, result; result = results[i]; i++) {
createMarker(result);
}
}
function createMarker(place) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: 'http://www.google.com/intl/en_us/mapfiles/ms/micons/yellow-dot.png'
});
google.maps.event.addListener(marker, 'click', function() {
service.getDetails(place, function(result, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
infoWindow.setContent(result.name);
infoWindow.open(map, marker);
});
});
}
initialize();
最佳答案
根据 Google Places Search API ( https://developers.google.com/places/web-service/search ),在 name
参数上:
name — One or more terms to be matched against the names of places, separated by a space character. Results will be restricted to those containing the passed name values. Note that a place may have additional names associated with it, beyond its listed name. The API will try to match the passed name value against all of these names. As a result, places may be returned in the results whose listed names do not match the search term, but whose associated names do.
我认为你应该在它之前使用 \
来转义你的空格字符:
var request = {
location: new google.maps.LatLng(33.7490, -84.3880),
radius: 50000,
name: "Best\ Buy"
};
注意:在你的 fiddle 中,我只有“Best Buy...”/“Best Buy”/“.. Best Buy...”结果,我没有得到你的问题
使用此更改更新 fiddle(以及小的 jsfiddle 改进):https://jsfiddle.net/fautepdf/16/
关于javascript - Google Places API - radarSearch 按名称搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35370339/