Question

I've a CLLocation point (lat/long) and I want to know if it is in a determined MKCoordinateRegion. I'd like to know in which region is a CLLocation point previously made.

Thanks.

I have found the solution.

MKCoordinateRegion region = self.mapView.region;

CLLocationCoordinate2D location = user.gpsposition.coordinate;
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);

if (
    location.latitude  >= northWestCorner.latitude && 
    location.latitude  <= southEastCorner.latitude &&

    location.longitude >= northWestCorner.longitude && 
    location.longitude <= southEastCorner.longitude
    )
{
    // User location (location) in the region - OK :-)
    NSLog(@"Center (%f, %f) span (%f, %f) user: (%f, %f)| IN!", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta, location.latitude, location.longitude);

}else {

    // User location (location) out of the region - NOT ok :-(
    NSLog(@"Center (%f, %f) span (%f, %f) user: (%f, %f)| OUT!", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta, location.latitude, location.longitude);
}
Was it helpful?

Solution

There was similar question. It was answered correctly - How to check if MKCoordinateRegion contains CLLocationCoordinate2D without using MKMapView?

Good Luck :)

OTHER TIPS

This seems to be correct but backwards. The latitude of a region's northwest corner will be greater than the center since latitude increases as you move north. The longitudes are correct. So the corners calculated here are switched, northwestCorner is actually the southwest corner and southeastCorner is actually the northeast corner. But the code works because the if statement is backwards too. It should be more like

CLLocationCoordinate2D newMapCenter = self.mapView.centerCoordinate;
CLLocationCoordinate2D startingCenter = self.startingRegion.center;
CLLocationCoordinate2D northWestCorner, southEastCorner;

northWestCorner.latitude = startingCenter.latitude + (self.startingRegion.span.latitudeDelta / 2.0);
northWestCorner.longitude = startingCenter.longitude - (self.startingRegion.span.longitudeDelta / 2.0);
southEastCorner.latitude = startingCenter.latitude - (self.startingRegion.span.latitudeDelta / 2.0);
southEastCorner.longitude = startingCenter.longitude + (self.startingRegion.span.longitudeDelta / 2.0);

if (newMapCenter.latitude <= northWestCorner.latitude && newMapCenter.latitude >= southEastCorner.latitude && newMapCenter.longitude >= northWestCorner.longitude && newMapCenter.longitude <= southEastCorner.longitude) {
    // still in original region
    NSLog(@"same region");
}
else
{
    // new region
    NSLog(@"new region");
}

So the answer is right, but also wrong. But it works so I guess it's more right.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top