Get TopLeft and BottomRight from MKCoordinateRegion MKMapView
Asked Answered
E

3

14

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?

Evacuee answered 17/3, 2010 at 18:13 Comment(0)
K
29

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);
Katharyn answered 17/3, 2010 at 23:3 Comment(2)
Thanks for correcting me, Andriy. I updated the code above so it should be correct now.Katharyn
Doesn't this code break if the region crosses either the International date line or the poles?Phillane
S
7

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)
Sibling answered 8/11, 2016 at 16:33 Comment(1)
@CarlHung Do you mean west and east were mixed up? If so I have fixed it.Sibling
F
0

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)

Fulkerson answered 18/8, 2010 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.