如何从 iPhone GPS 查找州位置?
我正在尝试添加到我的程序中,该程序可以在 GPS 中找到该人,但将值设置为 该人所在的州,例如:GPS 从他/她的 iphone 中定位该人,然后返回他们所在的州,所以说是加利福尼亚州,然后将州变量设置为加利福尼亚州作为字符串,如果有人有一个示例,我们将不胜感激,谢谢!
I'm trying to add to my program that locates the person in GPS but sets a value to
the State that person is in so example: GPS Locates person from his/her iphone then returns the state they are in so say its California then the state variable gets set to California as a string would someone have an example any help is appreciated thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 核心位置找到位置,然后使用 MKReverseGeocoder 从位置获取状态。
You can use Core Location to find the location and then use MKReverseGeocoder to get the state from the location.
您所要做的就是设置一个 CLLocationManager 来查找您当前的坐标。使用当前坐标,您需要使用 MKReverseGeoCoder 来查找您的位置。
What you have to do is setup a CLLocationManager that will find your current coordinates. With the current coordinates you need to use MKReverseGeoCoder to find your location.
只需对上述代码进行一些更正:
1. 使用 kABPersonAddressStateKey 将为您提供州,而不是城市
2. 如果您在编译 kABPersonAddressStateKey 时遇到问题,请将 AddressBook 框架添加到您的项目中并包含:
在您的 .m 文件中
Just a few corrections for the above code:
1. Using kABPersonAddressStateKey will give you the state, not the city
2. If you have problem with compiling kABPersonAddressStateKey, add the AddressBook framework to your project and include:
in your .m file
使用反向地理编码正是您所需要的,而且很简单。
给定一个包含 CLLocation *location 属性的类,只需使用我为我的一个项目编写的代码狙击手,就完成了。
请注意,您会对获取 d[@"State"] 而不是 d[@"FormattedAddressLines"] 更感兴趣。
另请注意,反向地理编码需要访问 Internet(它作为 Web 服务实现)并且受到容量限制。最有可能的是,每分钟不应超过几次调用。如果超出 Apple 设置的配额,您将收到错误消息。
为了您的方便,以下是 placeMark.addressDictionary 属性存储的 KV:
Using reverse geocoding is what you need and is straightforward.
Given a class that holds a CLLocation *location property, simply use this code sniper I wrote for one of my project, and you're done.
Note that you will be more interested in getting d[@"State"] rather than d[@"FormattedAddressLines"].
Also note that reverse geocoding requires internet access (it's implemented as a Web service) and is subject to volume limitation. Most likely, you shouldn't exceed more than a couple of calls per minute. If you exceed the quota set by Apple, you'll receive an error.
For your convenience, here are the KV stored by the placeMark.addressDictionary proprerty: