【发布时间】: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