谷歌地图:在特定国家/地区搜索

时间:2016-07-11 08:53:23

标签: php google-maps limit countries

我的PHP应用程序中有一个表单,用于搜索欧洲的位置:德国,法国,波兰,捷克共和国,西班牙,奥地利,罗马尼亚,意大利。

这是我的基本查询:

$address = "https://maps.googleapis.com/maps/api/geocode/json?address={$input}";

如果用户试图找到“曼海姆”,它应该返回德国曼海姆。 相反,它返回美国宾夕法尼亚州的一个点。我尝试添加此地址组件限制:&components=administrative_area:EU,但这不可靠,因为它不是找到曼海姆德国,而是指向300公里以外的地方:

https://www.google.ro/maps/dir/50.5320001,6.6364339/Mannheim/@50.0388399,8.4546225,7.25z/data=!4m8!4m7!1m0!1m5!1m1!1s0x4797cc24518e3f45:0xb1e4fe7aa406e687!2m2!1d8.4660395!2d49.4874592?hl=en

如果我追加, Germany

$address = "https://maps.googleapis.com/maps/api/geocode/json?address={$input}, Germany";

然后答案是正确的。

有没有办法指定一些搜索应该发生的国家?

最后一种方法是对每个国家进行单独搜索,但考虑到我搜索的10-12个国家,这将非常缓慢。

2 个答案:

答案 0 :(得分:0)

通过国家/地区限制来限制Google搜索结果,如下所示:

var options = {
        componentRestrictions: {country: 'DE'}
      };

并将选项参数放置在自动完成功能或您正在使用的其他功能中。

var autocomplete = new google.maps.places.Autocomplete(input,options);

答案 1 :(得分:0)

现在看来有可能:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-multiple-countries#maps_places_autocomplete_multiple_countries-javascript

  // 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",
  ]);