MaxPag
MaxPag

Reputation: 23

MKMapView userLocation return coordinates 0.0;0.0 on app's first launch

I'm currently developing an app that have use an MKMapView control. It is suppose to show the user's location via the blue dot (like the Maps application), but when the app is first installed on the device and then launched, you have the prompt asking the user for the authorization, but once accepted, the user's location is not showed on the map, and the coordinates returned are 0.0 ; 0.0.

Then, I turn off the app, remove it from multitasking and start it again, and the position is showed correctly and correct coordinates are returned.

I also use CoreLocation in order to be able to calculate the distance between two points and get the heading, and I set it up with a custom message for the prompt, which is show one out of two fresh installs of the app (I wonder why), I do not really care about that last problem, but it might help to know that detail.

What should I do to be able to get that blue do to show on the app's first launch?

Sincerely,

Max

Upvotes: 2

Views: 679

Answers (1)

Zee
Zee

Reputation: 1905

Yes, this happens sometimes with iPhone (not in simulator), I also experienced the same issue, and it crashes the app if you try to zoom the map to the given location. You may like to use following code to avoid this case. This will discard invalid GPS updates and eventually you will start getting valid data

if (mapView.userLocation.coordinate.latitude <= -180.0f
    || mapView.userLocation.coordinate.latitude >= 180.0f
    || mapView.userLocation.coordinate.longitude <= -180.0f
    || mapView.userLocation.coordinate.longitude >= 180.0f
    || (mapView.userLocation.coordinate.latitude == 0.0f 
    && mapView.userLocation.coordinate.longitude == 0.0f) ) {

    return; // or something else you may like to do
} 

// rest of the code

Upvotes: 1

Related Questions