当城市名称等于某个国家/地区的名称时,CLGeocoder会返回错误的结果(而不仅仅是) [英] CLGeocoder returns wrong results when city name is equal to some country's name (and not only)

查看:299
本文介绍了当城市名称等于某个国家/地区的名称时,CLGeocoder会返回错误的结果(而不仅仅是)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的某个应用中,我需要添加按名称查找城市的功能。我正在使用 CLGeocoder 来实现这一点,我希望它的行为与iOS天气应用相同。

In one of my apps I need to add an ability to find a city by its name. I am using CLGeocoder to achieve this and I want it to have a behaviour identical to iOS weather app.

是我的代码:

CLGeocoder().geocodeAddressString(searchBar.text!, completionHandler:{ (placemarks, error) -> Void in
    guard let nonNilMarks = placemarks else {return}
    for placemark in nonNilMarks {
        print("locality: \(placemark.locality)")
        print("name: \(placemark.name)")
        print("country: \(placemark.country)")
        print("formatted address: \(placemark.addressDictionary)")
    }
})

在大多数情况下,它的效果非常好。但是,我最近注意到一些失败的情况。以下是一些示例的输出:

It works very well in most cases. However, I recently noticed some cases when it fails. Below is the output for some examples:

locality: Optional("Milan")
name: Optional("Milan")
country: Optional("Italy")
formatted address: Optional([SubAdministrativeArea: Milan, State: Lombardy, CountryCode: IT, Country: Italy, Name: Milan, FormattedAddressLines: (
    Milan,
    Italy
), City: Milan])

这是正确的输出

德克萨斯州有一个名为意大利的小镇。如果我输入'Italy,TX',则输出为:

There is a town in Texas called Italy. If I type 'Italy, TX' the output is:

locality: Optional("Italy")
name: Optional("Italy")
country: Optional("United States")
formatted address: Optional([SubAdministrativeArea: Ellis, State: TX, CountryCode: US, Country: United States, Name: Italy, FormattedAddressLines: (
    "Italy, TX",
    "United States"
), City: Italy])



搜索'意大利' - 失败



当我输入'意大利'时,我只获得该国家的地方标记:

Searching for 'Italy' - FAILS

When I type just 'Italy' I only get place mark for the country:

locality: nil
name: Optional("Italy")
country: Optional("Italy")
formatted address: Optional([CountryCode: IT, Name: Italy, FormattedAddressLines: (
    Italy
), Country: Italy])



搜索'新加坡,新加坡' - WORKS



这与'Italy相似TX':

Searching for 'Singapore, Singapore' - WORKS

This is similar to the case with 'Italy, TX':

locality: Optional("Singapore")
name: Optional("Singapore")
country: Optional("Singapore")
formatted address: Optional([City: Singapore, CountryCode: SG, Name: Singapore, State: Singapore, FormattedAddressLines: (
    Singapore,
    Singapore
), Country: Singapore])



正在搜索'新加坡' - 失败



再次,似乎与'意大利'相似。它只找到一个国家:

Searching for 'Singapore' - FAILS

Again, seems similar to the case with 'Italy'. It only finds a country:

locality: nil
name: Optional("Singapore")
country: Optional("Singapore")
formatted address: Optional([CountryCode: SG, Name: Singapore, FormattedAddressLines: (
    Singapore
), Country: Singapore])



初步猜测



在这个阶段我虽然可能 geocodeAddressString:停止搜索是否找到名称等于搜索参数的国家/地区,因此我尝试将我的代码更改为:

Preliminary guess

At this stage I though that maybe geocodeAddressString: stops searching if it finds a country with the name equal to the search parameter, so I tried changing my code to :

let addrDict = [kABPersonAddressCityKey as NSString: searchBar.text!]
CLGeocoder().geocodeAddressDictionary(addrDict, completionHandler:{ (placemarks, error) -> Void in
    print(placemarks)
})

我认为通过将搜索字词限制为城市名称我会得到正确的结果。不幸的是,我得到完全相同的行为!

I thought that by restricting the search term to the city name I would get correct results. Unfortunately, I get the exact same behaviour!

那么我意识到我不仅遇到城市名称等于国家/地区名称。

And THEN I realised that I have problems not only with cases when city name is equal to the country name.

locality: nil
name: Optional("Tokyo")
country: Optional("Japan")
formatted address: Optional([CountryCode: JP, Name: Tokyo, State: Tokyo, FormattedAddressLines: (
    Tokyo,
    Japan
), Country: Japan])



结论



CLGeocoder 不仅可以省略结果(例如'意大利'),但它事实上它也可以告诉我一个地方没有地区(我相信上次我查了一张地图东京还是一个城市!)。

Conclusion

Not only can CLGeocoder omit results (like in the case of 'Italy') but it can also tell me that a place does not have a locality when, in fact, it does (I believe last time I checked a map Tokyo was still a city!).

有谁知道如何解决这些问题?

Does anyone know how I can resolve any of these issues?

天气应用以某种方式做到了 - 搜索'新加坡'或'意大利'那里给出了corr等结果。我将不胜感激任何帮助!

Weather app does it somehow - searching for 'Singapore' or 'Italy' there gives correct results. I will be grateful for any help!

P.S。虽然我使用Swift,但我将Objective-C添加为标签,因为我认为这个问题不是特定于Swift的。

P.S. Although I am using Swift I am adding Objective-C as a tag as I think that this question is not Swift specific.

推荐答案

用法你所展示的是完全正确的,我认为你无法做任何其他事情来改善结果。它仍然只是一些复杂的算法,试图根据一组规则和一些假设对结果进行排序。

Usage what you showed is completely right and I don't think there is anything else you can do to improve results. It is still just some complicated algorithm that tries to sort results based on set of rules and some assumption.

这很难过,我完全理解你的挫败感,因为我一直在在那里完成了。问题是,地理编码数据库显然并不完整,而苹果并不是领导这一领域的公司 - 谷歌就是这样。在这里您可以看到Geocoder介绍时的技术说明,说仍然是不受支持的国家/或仅部分支持的国家/地区。

That being sad, I understand your frustration completely because I've been there and done that. The problem is that Geocoding database is not obviously complete and Apple is not the company who leads this field - Google is. Here you can see technical note from when the Geocoder was introduced saying that there are still countries that are not supported, or just partially supported.

所以我的建议是,如果你想获得最好的结果(虽然仍然没有防止故障),请切换到Google Geolocation。对于相当多的请求它是免费的,之后它需要一些非常小的金额。对于我所经历的所有基本搜索,成功率约为99%,如果您提供更多信息,它只会变得更好。 API选项也相当广泛。

So my suggestion to you is that if you want to get best results (though still not fail-proof), switch to Google Geolocation. For quite great amount of requests it is free and after that it costs some very minor amounts. The success rate is around 99% for all the basic searches from what I experienced and it only gets better if you provide more information. The API options are also quite extensive.

以下是地理编码和反向地理编码的开发人员文档链接:
https://developers.google.com/maps/documentation/geocoding/intro
https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

Here are links for developer documentation for both Geocoding and Reverse Geocoding: https://developers.google.com/maps/documentation/geocoding/intro https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

希望它至少有一点帮助。

Hope it helped at least a little.

编辑:写完之后我做了一点研究,似乎我不是唯一一个提出此声明的人(还包括相同的不受支持的链接。)

After I wrote this I did a little research and it seems that I am not the only person to make this claim (also including the same unsupported link).

这篇关于当城市名称等于某个国家/地区的名称时,CLGeocoder会返回错误的结果(而不仅仅是)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆