Nick
Nick

Reputation: 1204

MKMapView - how to calculate square miles currently in view

To use various APIs that accept northEast and southWest coordinates to use as the "viewport", some have a maximum square miles limit for search.

I would like to calculate the square miles shown at the current MKMapView zoom level.

Upvotes: 0

Views: 456

Answers (1)

Nick
Nick

Reputation: 1204

Here is the solution:

- (float)numSquareMiles
{
    CLLocationCoordinate2D northEast, southWest;
    northEast = [mapView convertPoint:CGPointMake(mapView.frame.size.width, 0) toCoordinateFromView:mapView];
    southWest = [mapView convertPoint:CGPointMake(0, mapView.frame.size.height) toCoordinateFromView:mapView];


    float distanceLatInDegrees = northEast.latitude - southWest.latitude;
    float numLatMiles = 69.172 * distanceLatInDegrees;

    float distanceLonInDegrees = northEast.longitude - southWest.longitude;
    float numLonMiles = 69.172 * distanceLonInDegrees;

    float numSquareMiles = numLatMiles*numLonMiles;

    return numSquareMiles;
}

Upvotes: 1

Related Questions