• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS 正向地理编码返回不稳定、稀疏的结果

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:33:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在开发一个需要能够按名称搜索位置的应用程序,并且由于 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap