如何通过代码获取iPhone当前位置?
如何通过代码获取iPhone的当前位置?我需要使用 GPS 跟踪位置。
How do you get the current location of iPhone through code? I need to track the location using GPS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
开发人员网站上有(一如既往的核心功能)全面的文档,您可能会找到 这个例子很有用;
要记住的要点是;
1) 一旦您开始接收位置更新,它们就会异步到达,您需要在它们到达时做出响应。检查准确性和时间戳以决定是否要使用该位置。
2) 一旦获得所需位置,不要忘记停止接收更新(这可能不是第一个),否则会耗尽电池寿命。
3) 返回给您的第一个位置通常是最后缓存的位置,手机将以与之前修复时相同(可能很高)的精度报告此位置。因此,您可能会得到一个声称精确到 10 米的修复,但实际上是在昨天您距离当前位置数英里之外收到的。其座右铭是不要忘记检查时间戳 - 这会告诉您实际收到位置修复的时间。 以下是如何检查时间戳的示例。
希望有帮助。
There is (as always for core functionality) comprehensive documentation on the developer site and you may find this example useful;
The key points to remember are;
1) Once you start receiving location updates, they arrive asynchronously and you need to respond to them as and when they come in. Check the accuracy and timestamps to decide if you want to use the location or not.
2) Don't forget to stop receiving updates as soon as you have the location you want (this might not be the first one), otherwise you will drain the battery life.
3) The first location returned to you is often the LAST CACHED one, and the phone will report this location with the same (possibly high) accuracy it had when it got the fix before. So you might get a fix that claims to be accurate to 10 metres but it was actually received yesterday when you were miles away from your current location. The motto of this is DON'T FORGET TO CHECK THE TIMESTAMP - this tells you when the location fix was actually received. Here's an example of how to check the timestamp.
Hope that helps.
您正在寻找“CoreLocation”API。
这是教程
You're looking for the "CoreLocation" API.
Here's a tutorial
CLLocationManager 上的此页面有五个源代码示例
This page on CLLocationManager has five source code examples
下载此示例它将显示您当前的位置
download this example it will show you current location
如果您只需要获取设备的当前位置,则直接使用 Core Location 需要大量代码,因为您必须处理延迟,直到设备的 GPS 准确定位当前位置,这需要几秒钟的时间。 (CLLocationManager API 似乎是为需要连续位置更新的应用程序构建的,例如路线导航 GPS 导航应用程序。)
我建议您不要直接使用 CLLocationManager,而是考虑使用 开源组件,例如 INTULocationManager,它将为您处理所有这些工作,并使请求设备当前的一个或多个离散请求变得非常简单地点。
If you just need to get the device's current location, using Core Location directly requires a good deal of code because you must handle the delay until the device's GPS has an accurate fix on the current location, which takes a number of seconds. (The CLLocationManager API appears to be built for apps that need continuous location updates, like turn-by-turn GPS navigation apps.)
Instead of using CLLocationManager directly, I suggest you take a look at using an open source component such as INTULocationManager which will handle all of this work for you and make it trivially simple to request one or more discrete requests for the device's current location.
完整代码如下
1 拖动图
导入框架
一行给出委托
在这段代码的最后
self.yourmapreferencename.setRegion... // 这里的所有代码
import UIKit
导入地图工具包
导入核心位置
类 ViewController: UIViewController, CLLocationManagerDelegate {
}
}
full code is here below
1 drag map
import framework
give delegate
in last line of this code self.yourmapreferencename.setRegion...
// all code here
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
}
}
真正的解决方案可以在这里找到。 Z5 Concepts iOS 开发代码片段< /a>
它只需要一点点编码。
The real solution can be found here. Z5 Concepts iOS Development Code Snippet
It just requires a little bit of encoding.