【问题标题】:Google Places how to autocomplete and componentRestrictions search only from a specific country?Google Places 如何仅从特定国家/地区自动完成和 componentRestrictions 搜索?
【发布时间】:2018-04-26 16:14:47
【问题描述】:

我希望当用户在我的自动完成搜索中键入内容时,来自 Google 服务器的数据将仅与特定国家/地区相关。

我通过 Node.js 中的 API 调用他们的网络服务,如下所示:

var headers = {
  'User-Agent':       'Super Agent/0.0.1',
  'Content-Type':     'application/x-www-form-urlencoded'
};
// Configure the request
var options = {
  url: "https://maps.googleapis.com/maps/api/place/autocomplete/json",
  method: 'POST',
  headers: headers,
  qs: {
    key: gkey,
    input: input,
    types: ['(cities)'],
    componentRestrictions: {country: 'il'}
  }
};
// Start the request
request(options, function (error, response, body) {
  // returning the body to the frontend
});

请注意,我尝试过使用它,但没有任何效果。 我可以看到全球范围内的结果。

我试图搜索这个但没有解决这个问题。

【问题讨论】:

  • 自动完成网络服务应该是 GET,而不是 POST。你试过 GET 吗?您还可以查看用于 Web 服务的 NodeJs 客户端库:github.com/googlemaps/google-maps-services-js
  • 是的,我刚刚尝试使用 GET 并且效果很好。您最好回答它,以便其他面临此问题的人稍后阅读并查看解决方案。

标签: node.js google-maps autocomplete google-places-api


【解决方案1】:

Google Maps API 网络服务应该与 GET 方法一起使用,而不是 POST 方法。应该与 POST 一起使用的唯一 Web 服务是 Geolocation API。

你应该把代码改成

var headers = {
  'User-Agent':       'Super Agent/0.0.1',
  'Content-Type':     'application/x-www-form-urlencoded'
};
// Configure the request
var options = {
  url: "https://maps.googleapis.com/maps/api/place/autocomplete/json",
  method: 'GET',
  headers: headers,
  qs: {
    key: gkey,
    input: input,
    types: ['(cities)'],
    componentRestrictions: {country: 'IL'}
  }
};
// Start the request
request(options, function (error, response, body) {
  // returning the body to the frontend
});

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2017-08-28
    • 2016-04-02
    • 1970-01-01
    • 2017-10-20
    相关资源
    最近更新 更多