OGeek|极客世界-中国程序员成长平台

标题: iOS 正向地理编码返回不稳定、稀疏的结果 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:33
标题: iOS 正向地理编码返回不稳定、稀疏的结果

我正在开发一个需要能够按名称搜索位置的应用程序,并且由于 CLGeocoder,它看起来应该相当简单。我已经完成了所有设置,它似乎正在工作,但我得到的结果非常稀疏。

无论我输入什么,我最多会得到 1-2 个结果,通常根本没有。当我搜索“太平洋水族馆”时,它会正确找到它,但只有当我真正完成输入时:甚至只留下最后一个字母“c”,它什么都没有。输入我自己的地址不会返回任何内容,即使我完全输入它也是如此。

是这样的吗?当然 CLGeocoder 不是没用吗?我可以做些什么来增加返回结果的数量,或扩大可能的匹配范围?

下面是我正在使用的代码,连接到一个简单的 UITableViewUISearchBar 对...

-(void)searchBarUISearchBar *)searchBar textDidChangeNSString *)searchText {
    [_placeResults removeAllObjects];
    [_geocoder geocodeAddressString:searchText completionHandler:^(NSArray* placemarks, NSError* error){
        for (CLPlacemark* aPlacemark in placemarks) {
            [_placeResults addObject:aPlacemark];
        }
        [_mainTableView reloadData];
     }];
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
    return [_placeResults count];
}

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier"cell"];

    CLPlacemark *thisPlace = [_placeResults objectAtIndex:indexPath.row];
    [cell.labelTitle setText:[[[thisPlace addressDictionary] objectForKey"FormattedAddressLines"] objectAtIndex:0]];

    return cell;
}

正如我所说,它确实有点作用,所以我知道一切都正确连接。只是效果不太好

有什么建议吗?



Best Answer-推荐答案


我认为您对 CLGeocoder 的用途有错误的认识。它的目的是在地理位置坐标和它的全文标题之间进行转换。

此外,您是否阅读过文档? CLGeocoder 需要联系地理编码服务器。因此,在 textDidChange: 消息的每次迭代中调用它不仅是一种不当使用,而且效率非常低。

来自 Apple 的文档:

Applications should be conscious of how they use geocoding. Geocoding requests are rate-limited for each app, so making too many requests in a short period of time may cause some of the requests to fail. (When the maximum rate is exceeded, the geocoder returns an error object with the value kCLErrorNetwork to the associated completion handler.) Here are some rules of thumb for using this class effectively:

Send at most one geocoding request for any one user action.

If the user performs multiple actions that involve geocoding the same location, reuse the results from the initial geocoding request instead of starting individual requests for each action.

这里的想法是每个应用程序都受到 Apple 的速率限制。因此,您需要谨慎使用 API,并提交全文查询。

geocodeAddressString:completionHandler:

的摘录

After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request.

关于iOS 正向地理编码返回不稳定、稀疏的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25422121/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4