Get TopLeft and BottomRight from MKCoordinateRegion MKMapView

5.9k views Asked by At

I checked the properties in documentation for MKCoordinateRegion, MKCoordinateSpan and MKMapView to see there is a way to get the TopLeft and BottomRight Lat Long from the map view and I didn't find any. I know that the span gives me the Lat long delta but is there a way to get the actual TopLeft and BottomRight lat longs from map view without having to do weird calculations?

I found this.

Not sure if that is accurate enough. Any votes for that?

3

There are 3 answers

0
Joseph Tura On

Are you sure you got the +- right? I did not get useful results with that. When I switched the +-, I did. Might be my code is flawed somewhere else, though ;)

Longitude is given as an angular measurement ranging from 0° at the Prime Meridian to +180° eastward and −180° westward. The Greek letter λ (lambda),[3][4] is used to denote the location of a place on Earth east or west of the Prime Meridian.

Technically, latitude is an angular measurement in degrees (marked with °) ranging from 0° at the equator (low latitude) to 90° at the poles (90° N or +90° for the North Pole and 90° S or −90° for the South Pole).

(Wikipedia)

2
Ole Begemann On

I don't think these calculations qualify as weird:

CLLocationCoordinate2D center = region.center;
CLLocationCoordinate2D northWestCorner, southEastCorner;
northWestCorner.latitude  = center.latitude  + (region.span.latitudeDelta  / 2.0);
northWestCorner.longitude = center.longitude - (region.span.longitudeDelta / 2.0);
southEastCorner.latitude  = center.latitude  - (region.span.latitudeDelta  / 2.0);
southEastCorner.longitude = center.longitude + (region.span.longitudeDelta / 2.0);
1
Avt On

Straightforward calculations implemented in Swift 3.0 as extension:

extension MKCoordinateRegion {
    var northWest: CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: center.latitude + span.latitudeDelta  / 2,
                                      longitude: center.longitude - span.longitudeDelta / 2)
    }
    var northEast: CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: center.latitude + span.latitudeDelta  / 2,
                                      longitude: center.longitude + span.longitudeDelta / 2)
    }
    var southWest: CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: center.latitude - span.latitudeDelta  / 2,
                                      longitude: center.longitude - span.longitudeDelta / 2)
    }
    var southEast: CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: center.latitude - span.latitudeDelta  / 2,
                                      longitude: center.longitude + span.longitudeDelta / 2)
    }
}

Usage:

var region: MKCoordinateRegion = ... some region here
print("North - West", region.northWest)