如何检查CLLocation在iOS中的有效性 [英] How to check validity of CLLocation in iOS

查看:199
本文介绍了如何检查CLLocation在iOS中的有效性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS中检查 CLLocation 的有效性?



其实这是我的情况,



我只是创建一个新地图

  mapView = [[MKMapView alloc] initWithFrame :CGRectMake(10,30,300,380)]; 
mapView.showsUserLocation = YES;
[mapView setDelegate:self];

然后我想检查用户位置的有效性

  mapView.userLocation.location 

在使用用户位置时出现此错误

 'NSInvalidArgumentException',原因:协调中心无效-180.00000000,-180.00000000'$ b提前感谢〜

解决方案 / div>

根据Abizern的注释,你不应该假定在将 showsUserLocation 设置为 YES



当位置可用时,地图视图将调用其委托方法 didUpdateUserLocation

(如果获取位置失败, didFailToLocateUserWithError 委托方法将被调用。)



didUpdateUserLocation 方法之外,有几种方法可以检查位置是否可以使用:




  • 检查 userLocation.location 是否为 nil 。如果是,该位置尚未获得,它失败,或 showsUserLocation NO

  • 如果位置不是 nil ,您可以查看 code>属性,并且可以使用 CLLocationCoordinate2DIsValid 函数。注意,坐标0,0是有效的(这通常发生在位置为nil时)。



我注意到即使在 didUpdateUserLocation 委托方法, userLocation.location 可以 nil



这似乎发生在第一次运行应用程序时,以及将 showsUserLocation 设置为 YES 。此时,iOS会提示用户允许应用使用您的位置?而同时委托被调用(即使用户尚未响应提示并且位置尚未确定)。



因此,在在该委托方法的顶部,您还应该检查 userLocation.location nil





顺便说一句,在您的代码中,您可能需要在设置 showsUserLocation 之前设置委托。


How to check validity of CLLocation in iOS?

In fact this is my situation,

I just create a new map

mapView = [[MKMapView alloc] initWithFrame:CGRectMake(10, 30, 300, 380)];
mapView.showsUserLocation = YES;
[mapView setDelegate:self];

And then I want to check the validity of the user location

mapView.userLocation.location 

Since I get this error when using the user location

'NSInvalidArgumentException', reason: 'Invalid Coordinate -180.00000000, -180.00000000'

Thanks in advance~

解决方案

As Abizern's comment implies, you shouldn't assume the location will be ready to use immediately after setting showsUserLocation to YES.

When the location is available, the map view will call its delegate method didUpdateUserLocation.
(If getting the location fails, the didFailToLocateUserWithError delegate method will be called.)

Outside of the didUpdateUserLocation method, here are a couple of ways to check if the location is ok to use:

  • Check if userLocation.location is nil. If it is, the location hasn't been obtained yet, it failed, or showsUserLocation is NO.
  • If the location is not nil, then you can look at the coordinate property and that can be checked (if you think it might not be valid) using the CLLocationCoordinate2DIsValid function. Note however that coordinate 0,0 is valid (this generally happens when location is nil).

I have noticed that even in the didUpdateUserLocation delegate method, userLocation.location can be nil.

This seems to happen when running an app for the first time and after setting showsUserLocation to YES. At that point, iOS prompts the user with "Allow app to use your location?" while at the same time the delegate is called (even though the user hasn't yet responded to the prompt and the location hasn't been determined).

So at the top of that delegate method, you should also check if userLocation.location is nil.


By the way, in your code you may want to set the delegate before setting showsUserLocation.

这篇关于如何检查CLLocation在iOS中的有效性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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