我的PHP应用程序中有一个表单,用于搜索欧洲的位置:德国,法国,波兰,捷克共和国,西班牙,奥地利,罗马尼亚,意大利。
这是我的基本查询:
$address = "https://maps.googleapis.com/maps/api/geocode/json?address={$input}";
如果用户试图找到“曼海姆”,它应该返回德国曼海姆。
相反,它返回美国宾夕法尼亚州的一个点。我尝试添加此地址组件限制:&components=administrative_area:EU,
但这不可靠,因为它不是找到曼海姆德国,而是指向300公里以外的地方:
如果我追加, Germany
:
$address = "https://maps.googleapis.com/maps/api/geocode/json?address={$input}, Germany";
然后答案是正确的。
有没有办法指定一些搜索应该发生的国家?
最后一种方法是对每个国家进行单独搜索,但考虑到我搜索的10-12个国家,这将非常缓慢。
答案 0 :(得分:0)
通过国家/地区限制来限制Google搜索结果,如下所示:
var options = {
componentRestrictions: {country: 'DE'}
};
并将选项参数放置在自动完成功能或您正在使用的其他功能中。
var autocomplete = new google.maps.places.Autocomplete(input,options);
答案 1 :(得分:0)
// Sets a listener on a given radio button. The radio buttons specify
// the countries used to restrict the autocomplete search.
function setupClickListener(id, countries) {
const radioButton = document.getElementById(id);
radioButton.addEventListener("click", () => {
autocomplete.setComponentRestrictions({ country: countries });
});
}
setupClickListener("changecountry-usa", "us");
setupClickListener("changecountry-usa-and-uot", [
"us",
"pr",
"vi",
"gu",
"mp",
]);