iphone - 我如何知道 MKMapView 上的 setregion 何时不会触发 RegionWillChange/regionDidChange

标签 iphone cocoa-touch mapkit mkmapview

我正在编写一些 iPhone MapKit 代码,本地图边界发生变化时(基本上是对外部服务器的一些查询),这些代码会触发一些事件。有一种边缘情况我似乎无法解决。当用户输入新地址时,我使用 setRegion 放大该位置,然后计算 map 的边界并使用这些坐标查询我的外部服务器。我总是调用 setRegion 但是,有一种边缘情况它不会触发。也就是说,当用户神奇地碰巧选择位于 map 当前中心的完全相同的地址时,当该 map 缩放到完全相同的区域时。当然这种可能性很少见,但通过以下代码很容易实现:

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];

这相当于允许用户在当前 map 的中心放置一个图钉,并基于此向我的服务器发起查询。不幸的是,由于代码逻辑取决于区域更改(我这样做是为了让用户可以轻松地在 map 上平移,而不必显式按下刷新按钮),因此我需要解决方法。我如何知道 setRegion 何时被调用但它不会触发 RegionWillChange/RegionDidChange。我可以如何重写该函数吗?

编辑: 当我评论第一个答案时,我尝试使用以下代码来检测该区域何时不会更改:

//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];

不幸的是,当我将 searchLocation 设置为当前 map 的中心时,我得到以下结果:

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

在不同的情况下,错误略有不同,但一般来说,即使区域略有不同(例如 setRegion 或更重要的是,regionDidChange),也不会触发。谁能解释为什么?或者我怎样才能正确地检测到这一点。注意:我也尝试过使用mapView.centerCooperative,但我得到了类似的错误,我只想知道该区域何时不会改变(如果centerCoordinate相同,则缩放级别/区域仍然可以不同)。

最佳答案

如果检测到将 map 设置为当前中心的点,为什么不直接从设置新 map 位置的代码中手动调用 RegionWill/Did 更改?

关于iphone - 我如何知道 MKMapView 上的 setregion 何时不会触发 RegionWillChange/regionDidChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2088118/

相关文章:

iphone - 自动 "canShowCallOut"注释iPhone

iphone - 核心数据保存问题: can't update transformable attribute (NSArray)

iPhone UIScrollView/setContentOffset 怪异

iphone - UIPageControl 前进超过一页 - 有可能吗?

html - 在 iOS 上将网页精简为文本( Objective-C )

ios - 如果附近的 map 引脚太多,则 MKMapView 聚类

iphone - 选择国家时在国家(Mapkit)上绘制颜色

ios - 在 iPhone 4S 上,ViewController 上的更改不会从 didSelectRowAtIndexPath 持续到 cellForRowAtIndexPath

iphone - 苹果的推送通知服务可靠吗?

iphone - 如何实现loadView?