代码之家  ›  专栏  ›  技术社区  ›  Raz

谷歌将如何仅从特定国家进行自动完成和组件限制搜索?

  •  0
  • Raz  · 技术社区  · 6 年前

    我希望当用户在我的自动完成搜索中键入时,来自谷歌服务器的数据将只与特定国家相关。

    我通过节点中的API调用他们的webservice。js如下:

    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
    });
    

    请注意,我已经尝试过使用它,但没有任何效果。 我可以看到全世界的结果。

    我曾试图寻找这个问题,但没有任何解决办法。

    1 回复  |  直到 6 年前
        1
  •  2
  •   xomena    6 年前

    Google Maps API web服务应该与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
    });
    

    我希望这有帮助!