I am working on some iPhone MapKit code that disables some events when the map borders change (basically, some requests to an external server). There is one edge case that I cannot solve. When users enter a new address, I use setRegion to increase at this location, and then I calculate the map borders and query my external server with these coordinates. I always call setRegion, but there is one edge case where it fails. That is, when the user magically selects the same address that is in the current center of the map, when this map scales exactly in one region. Of course, this feature is rare, but it is easily achievable with the following code:
CLLocation *centerOfMap = [[CLLocation alloc] initWithLatitude:mapView.centerCoordinate.latitude longitude:mapView.centerCoordinate.longitude]; mySearchLocation.searchLocation = centerOfMap; //Recenter and zoom map in on search location MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}}; region.center = mySearchLocation.searchLocation.coordinate;region.span.longitudeDelta = 0.01f; region.span.latitudeDelta = 0.01f; [self.mapView setRegion:region animated:YES];
This would be equivalent to letting the user reset the pin in the center of the current map and drop the request to my server based on this. Unfortunately, since the logic of the code depends on the change in the area (which I did so that users can easily move around the map and do not have to press the update button explicitly), I need work. How to find out when setRegion was called, but it will not start regionWillChange / regionDidChange. Can I override a function like?
EDIT: When I commented on the first answer, I tried to detect when the region did not change using the following code:
//Recenter and zoom map in on search location MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}}; region.center = mySearchLocation.searchLocation.coordinate; region.span.longitudeDelta = 0.01f; region.span.latitudeDelta = 0.01f; region = [mapView regionThatFits:region]; //Not sure if this is necessary NSLog(@"diff in latitude of center %f", (mapView.region.center.latitude - region.center.latitude)); NSLog(@"diff in longitude of center %f", (mapView.region.center.longitude - region.center.longitude)); NSLog(@"diff in latitude span %f", (mapView.region.span.latitudeDelta - region.span.latitudeDelta)); NSLog(@"diff in longitude span %f", (mapView.region.span.longitudeDelta - region.span.longitudeDelta)); [self.mapView setRegion:region animated:YES];
Unfortunately, when I establish that searchLocation is the center of the current map, I get the following results:
diff in latitude of center 0.000000 diff in longitude of center 0.000016 diff in latitude span -0.000000 diff in longitude span 0.000000
In different cases, the error is slightly different, but, generally speaking, even if the region is slightly different from this setRegion or more, regionDidChange does not work. Can anyone explain why? Or how I could find it right. Note. I also tried using mapView.centerCoordinate, but I get similar errors, and I only want to know when the area does not change (if centerCoordinate matches the zoom level / area can still be different).