何时/何处开始更新位置?

发布于 2024-09-26 15:41:12 字数 842 浏览 5 评论 0原文

位置的确定需要一些时间。位置管理器应该在何时何地启动?

现在,我在加载结果视图(需要位置)之前开始更新一个视图的位置。如果用户快速点击,我会得到 0.0 坐标。

为了获得正确的时机,应该在结果视图之前调用 startUpdatingLocation 三个视图。这里的问题是我必须通过这三个视图控制器传递值。

接下来的事情是我还需要另一个视图中的位置(不是上面提到的视图)。我想我会创建一个自己的位置侦听器类。但是这个类什么时候会被实例化并传递坐标(经度、纬度)呢?协议对我没有帮助,因为只有创建我的位置侦听器的类才能获取结果。

我读了一点并提出了以下可能的解决方案:

1)使用通知中心(请参阅此解决方案

2)在AppDelegate中创建我的位置管理器的属性

1)我会遇到需要结果的视图的问题之前不会被创建。

2) 的实施会是什么样子?我在应用程序委托中创建位置管理器的实例,然后访问一些 ivars,例如 AppDelegate 的纬度/经度?如果我采用此处描述的解决方案,我将必须实施每次CLLocationManagerDelegate。开设一堂课来完成这项工作不是更好吗?

也许您有第三种解决方案?

The determination of a position takes some time. When and where should the location manager be started?

Now I'm starting the location update one view before the result view (which needs the location) is loaded. If the user taps to fast I get 0.0 coordinates.

To get the right timing the startUpdatingLocation should be called three views before the result view. The problem here is I would have to pass the value trough this three view controllers.

The next thing is that I need the location also in another view (not the views mentioned above). I thought I will create an own location listener class. But when will this class be instantiated and the coordinates (longitude, latitude) passed through? A protocol doesn't help me, because only the class which created my location listener will get the results back.

I read a bit and come up with the following possible solutions:

1) Use of the Notification Center (see this solution)

2) Create a property of my location manager in the AppDelegate

At 1) I would have the problem that the view, which needs the results wouldn't have be created before.

How would the implementation of 2) would look like? I create an instance of the location manager in the app delegate and then I access some ivars like latitude/longitude of the AppDelegate? If I would take the solution described here I would have to implement the CLLocationManagerDelegate every time. Wouldn't it be better to have one class for doing that?

Perhaps you have a third solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2024-10-03 15:41:12

第三种解决方案可能是使用单例类,将位置的纬度和经度存储在 CLLocationCooperative2D 中。然后,您可以使用以下代码从项目中的任何视图访问存储的坐标

DataController* dataController = [DataController sharedDataController];
CLLocationCoordinate2D currentLocationCoordinate = dataController.coordinate;

A third solution may be to use a singleton class where you store the latitude and longitude of the location in a CLLocationCoordinate2D. You can then use the following code to access the stored coordinate from any view in your project

DataController* dataController = [DataController sharedDataController];
CLLocationCoordinate2D currentLocationCoordinate = dataController.coordinate;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文