是否可以使用 Foursquare API 搜索包含字符串的位置?

Is it possible using Foursquare API search places containing string?

我想要像 Google 搜索那样的搜索结果。

在这里,如果我搜索 'tokyo',它会给我四个包含地点 'tokyo' 的结果。 我想使用 Foursquare API 实现相同的功能。目前我可以从当前位置或给定位置找到附近的地方。

您需要使用 https://api.foursquare.com/v2/venues/search 端点并使用 查询参数 以便它 return 根据您的关键字进行匹配。由于您在 iOS,您可以使用 UITableView 将结果显示为搜索建议或符合您要求的其他库。因为它会像一个 autocomplete/autosuggest 搜索,我建议你尝试在文本字段的更改事件上延迟调用端点。

AFNetworking 库用于 iOS 的示例:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:FOURSQUARE_CLIENTID forKey:@"client_id"];
[params setObject:FOURSQUARE_CLIENTSECRET forKey:@"client_secret"];
[params setObject:searchTextField.text forKey:@"query"];
[params setObject:LatLong forKey:@"ll"];
[params setObject:FOURSQUARE_VERSION forKey:@"v"];

[manager GET:@"https://api.foursquare.com/v2/venues/search" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
    //Process the JSON response here and output each venue name as search suggestion
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

我建议您阅读更多关于 FourSquare documentation 以及如何处理 JSON 结果并将它们显示到 UITableView 的信息(如果您还不知道如何处理的话)。

另请注意,您仍然必须提供 "ll" 作为参数,因为您只能使用端点查询附近的地点。

用于全局搜索,不使用 "ll" 或 "nearby" 参数。您可以尝试使用根据文档的全局意图:

Finds the most globally relevant venues for the search, independent of location. Ignores all other parameters other than query and limit.