开发者

Sanity Check an MKCoordinateRegion

Is there any way t开发者_如何学运维o 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.


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜