How to check validity of CLLocation in iOS

2020-02-06 02:29发布

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~

2条回答
成全新的幸福
2楼-- · 2020-02-06 02:37

To build on Anna K's explanation, it's general good practice to check the validity of a CLLocationCoordinate2DIsValid(YOURLOCATION).

If you are getting lats and longs from a web service or a SQLite database, you should check and make sure the location is valid before trying to add an annotation to the map. Here's an example of this in action:

for (int i = 0; i < [yourArray count]; i++) {

    YourOBJ *obj = [yourArray objectAtIndex:i];

    yourCLLocation2D.latitude = obj.latitude;
    yourCLLocation2D.longitude = obj.longitude;

    AnnotationPin *anno = [[AnnotationPin alloc] initWithCoordinate: yourCLLocation2D];

    if (CLLocationCoordinate2DIsValid(yourCLLocation2D))
    {
        anno.lat = obj.latitude;//Maybe you want to store lat
        anno.lon = obj.longitude;//and lon
        anno.title = obj.name//define the title
        anno.subtitle = obj.info//and subtitle for your annotation callout
        anno.yourLocation = yourCLLocation2D;//Some of these aren't necessary
        anno.tag = i;//but can really help to define and track pins

        [map addAnnotation:anno];
    }

    [anno release];
}

For all those who are having problems where you add a bunch of pins to a map successfully, and with the same code try to add another set of pins but the app crashes, try this asap. For those who have a similar situation in this example, but haven't ran into any problems, do this anyway! Your users might add an incorrect lat/lon combo and crash the app if you give them that functionality.

Remember, (0,0) is a valid coordinate, so don't worry about this logic removing those if you are concerned about that. This is for those (-200,75) type coordinates that don't physically exist on planet Earth.

查看更多
做个烂人
3楼-- · 2020-02-06 02:39

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.

查看更多
登录 后发表回答