How to check CLLocation on iOS

How to check the correctness of CLLocation in iOS?

This is actually 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 correct location of the user

 mapView.userLocation.location 

Since I get this error when using user location

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

Thanks in advance ~

+5
source share
2 answers

As follows from Abizern's comment, you should not assume that the location will be ready for use immediately after installing showsUserLocation in YES .

When the location is available, the map view will call its delegation method didUpdateUserLocation .
(If the location cannot be obtained, the delegation method didFailToLocateUserWithError will be called.)

Outside of the didUpdateUserLocation method, here are some ways to check if the location is suitable:

  • Check if userLocation.location nil . If so, the location has not yet been received, it failed, or showsUserLocation is NO .
  • If the location is not nil , you can look at the coordinate property and check (if you think this might be wrong) with CLLocationCoordinate2DIsValid . Note, however, that the coordinate 0,0 is valid (this usually happens when the location is zero).

I noticed that even in the didUpdateUserLocation delegate didUpdateUserLocation userLocation.location could be nil .

This is similar to launching the application for the first time and after installing showsUserLocation on YES . At this point, iOS prompts the user to "Allow the application to use your location"? and at the same time, a delegate is called (although the user has not yet answered the request and the location has not been determined).

At the top of this delegate method, you should also check if userLocation.location nil .


By the way, in your code you can set a delegate before installing showsUserLocation .

+10
source

To rely on Anna K's explanation, the general recommendation is to check the correctness of CLLocationCoordinate2DIsValid (YOURLOCATION).

If you get lats and longs from a web service or SQLite database, you should check and make sure the location is valid before trying to add annotation to the map. Here is 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 have problems, when you add a bunch of contacts to the card successfully, and with the same code try to add another set of contacts, but the application will fail, try this as soon as possible. For those who have a similar situation in this example, but have not encountered any problems, do it anyway! Your users may add the wrong lat / lon combination and application crash if you provide them with this functionality.

Remember that (0,0) is the actual coordinate, so don't worry about this logic by deleting them if that bothers you. This is for those (-200.75) coordinate types that do not physically exist on planet Earth.

+4
source

Source: https://habr.com/ru/post/910575/


All Articles