How to know when setregion on MKMapView will not start regionWillChange / regionDidChange

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).

+4
source share
3 answers

Why donโ€™t you just call the region / Change manually from the code that sets the new location of the map if it detects that the point on which the map will be installed will be the current center?

0
source

This is a hacky workaround that seems to work well for me.

 MKCoordinateRegion currentRegion = mapView.region; // Capture the instances where setRegion will not be fired BOOL regionLatitudeSame = (int)(currentRegion.center.latitude*1000000) == (int)(region.center.latitude*1000000); BOOL regionLongitudeSame = (int)(currentRegion.center.longitude*1000000) == (int)(region.center.longitude*1000000); BOOL regionSame = regionLatitudeSame && regionLongitudeSame; if (regionSame) { // Safe to assume that regionWillChange/regionDidChange WON'T be called } 

As you probably noticed, I made the assumption that the accuracy after the sixth decimal place is too small ( up to 11 mm ) for the visual change that is required on the map (even at the highest zoom level).

0
source

The following codes apply to me:

  let predictRect = mapView.convertRegion(mapView.regionThatFits(THE_REGION_YOU_WANT_TO_SET), toRectTo: mapView) if predictRect.origin.x.rounded() == 0 && predictRect.origin.y.rounded() == 0 && predictRect.size.width.rounded() == mapView.bounds.width && predictRect.size.height.rounded() == mapView.bounds.height { debugPrint("setRegion same, will not trigger region change event") } 
0
source

All Articles