Hi Guys I am Developing Map-view Base app. I Have almost done everything with map-view but I unable to compare two different map-view regions and find new area of map-view. For example, if the user drags the map, I want to find how much the region has changed.
4 Answers
partial answer. Equatable compliance for MKCoordinateRegion
extension MKCoordinateRegion: Equatable
{
public static func == (lhs: MKCoordinateRegion, rhs: MKCoordinateRegion) -> Bool
{
if lhs.center.latitude != rhs.center.latitude || lhs.center.longitude != rhs.center.longitude
{
return false
}
if lhs.span.latitudeDelta != rhs.span.latitudeDelta || lhs.span.longitudeDelta != rhs.span.longitudeDelta
{
return false
}
return true
}
}

- 5,486
- 5
- 41
- 66
First you need to find the initial map region. Say your map is named mapView...you can first find this by (in your viewDidLoad):
CLLocationCoordinate2D center = mapView.centerCoordinate;
CLLocationDegrees lat = center.latitude;
CLLocationDegrees lon = center.longitude;
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span = region.span;
//Assuming they have been declared as instance variables of type double
current_lat_low = lat - span.latitudeDelta / 2.0;
current_lat_high = lat + span.latitudeDelta / 2.0;
current_lon_low = lon - span.longitudeDelta / 2.0;
current_lon_high = lon + span.longitudeDelta / 2.0;
This will give you the initial area of the map shown. Then in
- (void)mapView:(MKMapView*)mapView regionDidChangeAnimated:(BOOL)animated
{
CLLocationCoordinate2D center = mapView.centerCoordinate;
CLLocationDegrees lat = center.latitude;
CLLocationDegrees lon = center.longitude;
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span = region.span;
double lat_low = lat - span.latitudeDelta / 2.0;
double lat_high = lat + span.latitudeDelta / 2.0;
double lon_low = lon - span.longitudeDelta / 2.0;
double lon_high = lon + span.longitudeDelta / 2.0;
//do any work comparing the initial lat/lons with the new values
.....
//set current lat/lon to be the new lat/lon after work is complete
current_lat_low = lat_low;
current_lat_high = lat_high;
current_lon_low = lon_low;
current_lon_high = lon_high;
}

- 1,705
- 1
- 18
- 24
I guess it really depends on what you are trying to do. You could, for example, be trying to compare the difference between the centers of the two regions. However, you mentioned area.
An MKCoordinateRegion
is just a rectangle and unless the zoom level changes will just have the same total area (LxW units squared). So I'll point you to a CLCircularRegion whose area can be computed using its radius which together with the center is an exposed property.
There are a number of ways to get the circular region from your map data. Your question does not make clear whether you are interested in the distance between the center of the areas but there should be plenty of SO answers out there on finding that difference.
If one of these scenarios is not exactly what you want to achieve, leave me a comment.
(source)

- 12,895
- 5
- 82
- 100
This answer shows you how to compare two map points. You could use this on the centre of your map regions: link.

- 1
- 1

- 1,272
- 1
- 13
- 32
-
1This is not an answer to the question it compares the distance between two points not two regions. – Rog Aug 20 '14 at 08:14