nevan king
nevan king

Reputation: 113747

Sanity Check an MKCoordinateRegion

Is there any way to quickly test whether an MKCoordinateRegion is good or not? I've swapped latitude for longitude and caused an application crash. I'd like to be able to see whether it's possible to perform a setRegion before I actually do it. Will MKCoordinateRegionMake test the values I give it?

Thanks.

Upvotes: 7

Views: 4146

Answers (2)

nevan king
nevan king

Reputation: 113747

It turns out I had swapped my latitude and longitude somewhere. Here's the code I ended up using:

// check for sane span values
if (currentRegion.span.latitudeDelta <= 0.0f || currentRegion.span.longitudeDelta <= 0.0f) {
    currentRegion.span.latitudeDelta = 1.0f;
    currentRegion.span.longitudeDelta = 1.0f;
}
// check for sane center values
if (currentRegion.center.latitude > 90.0f || currentRegion.center.latitude < -90.0f ||
    currentRegion.center.longitude > 180.0f || currentRegion.center.longitude < -180.0f
    ) {
    // Take me to Tokyo.
    currentRegion.center.latitude = 35.4f;
    currentRegion.center.longitude = 139.4f;
}

Upvotes: 10

TechZen
TechZen

Reputation: 64428

I haven't fiddle with the map much so take this with a grain of salt.

It sounds like you need to put a range check on your values for latitude, longitude and span before you use them. Latitude and longitude should in all cases have values between -180 and 180.

Edit01:

I don't think that swapping latitude and longitude could cause a crash. They both have exactly the same units (degrees) and ranges(-180-->180). All valid values of latitude are also valid values for longitude and vice versa. For this reason, MKCoordinateRegionMake won't be able to tell that you've swapped the values because they would still be valid coordinates, just not anywhere near where you wanted.

I think your problem is elsewhere.

Upvotes: -1

Related Questions