我有一个使用MKMapView的简单iphone应用程序。它有一个分段控件,可以调整区域进行缩放(街道,社区,城市,州,世界)。它工作正常,但如果我缩小到州级并回到街道,我注意到中心点已经移动。
以下是在区域更改之前和之后调用的两个委托方法以及由分段控件调用的changeZoom方法:
- (void)mapView:(MKMapView *)mView regionDidChangeAnimated:(BOOL)animated { MKCoordinateRegion region = [mapView region]; NSLog(@"Region DID change. Center is now %f,%f, Deltas=%f,%f", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta); } - (void)mapView:(MKMapView *)mView regionWillChangeAnimated:(BOOL)animated { MKCoordinateRegion region = [mapView region]; NSLog(@"Region WILL change. Center is now %f,%f, Deltas=%f,%f", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta); } - (IBAction)changeZoom:(id)sender { UISegmentedControl *sc = (UISegmentedControl *)sender; float mile_range = 1; NSLog(@"changeZoom called. MapView currently is Center=%f,%f Deltas=%f,%f", mapView.region.center.latitude, mapView.region.center.longitude, mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta ); switch ( [sc selectedSegmentIndex] ) { case 0: /* Street */ mile_range=1; break; case 1: /* Hood */ mile_range=2; break; case 2: /* City */ mile_range=30; break; case 3: /* State */ mile_range=500; break; case 4: /* World */ mile_range=4000; break; default: /* Error */ NSLog(@"Unknown segment selected from the UISegmentedControl!!!!!!!!!!!!!!"); return; break; } originalZoomCenter = mapView.region.center; MKCoordinateRegion region; region = MKCoordinateRegionMakeWithDistance( originalZoomCenter, mile_range/MILES_PER_METER, mile_range/MILES_PER_METER ); region = [mapView regionThatFits: region]; NSLog(@"Calling setRegion Center=%f,%f Deltas=%f,%f", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta ); [mapView setRegion: region animated: YES]; NSLog(@"After setRegion Center=%f,%f Deltas=%f,%f", mapView.region.center.latitude, mapView.region.center.longitude, mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta ); }
如果我运行应用程序并且它在我当前位置的街道缩放级别开始,然后我点击缩小到州级别并回到街道级别,我在控制台中得到以下输出:
Region WILL change. Center is now 30.145127,-40.078125, Deltas=151.851332,225.000000 Region DID change. Center is now 37.331676,-122.030725, Deltas=0.024842,0.027466 changeZoom called. MapView currently is Center=37.331676,-122.030725 Deltas=0.024842,0.027466 Calling setRegion Center=37.331676,-122.030725 Deltas=12.708100,14.062500 Region WILL change. Center is now 37.331676,-122.030725, Deltas=0.024842,0.027466 After setRegion Center=37.331676,-122.030725 Deltas=0.024842,0.027466 Region DID change. Center is now 37.335224,-122.036133, Deltas=12.707505,14.062500 changeZoom called. MapView currently is Center=37.335224,-122.036133 Deltas=12.707505,14.062500 Calling setRegion Center=37.335224,-122.036133 Deltas=0.024841,0.027466 Region WILL change. Center is now 37.335224,-122.036133, Deltas=12.707505,14.062500 After setRegion Center=37.335224,-122.036133 Deltas=12.707505,14.062500 Region DID change. Center is now 37.335224,-122.036133, Deltas=0.024841,0.027466
因此,我在代码中进行的所有调用都按预期进行,并且中心没有变化,但是当我监视MKMapViewDelegate方法regionDidChangeAnimated时,我看到中心被移动了。如果我在那里设置断点,我会看到一个如下所示的堆栈:
#0 0x00034c4c in -[MapViewController mapView:regionDidChangeAnimated:] at MapViewController.m:209 #1 0x01fbf7bf in -[MKMapView _didChangeRegionMidstream:] #2 0x003e11f5 in -[UIView(Gestures) animator:stopAnimation:] #3 0x003743be in -[UIAnimator stopAnimation:] #4 0x00374154 in -[UIAnimator(Static) _advance:] #5 0x0264d719 in HeartbeatTimerCallback #6 0x01d28ac0 in CFRunLoopRunSpecific #7 0x01d27c48 in CFRunLoopRunInMode #8 0x0264a78d in GSEventRunModal #9 0x0264a852 in GSEventRun #10 0x00307003 in UIApplicationMain #11 0x00002194 in main at main.m:14
这并没有真正帮助我理解为什么中心会移动。我已经在模拟器和设备上运行了它,这让我有点疯狂。
如果有人对导致中心转移的原因有任何了解,我将不胜感激。
答案 0 :(得分:3)
根据我的经验,在使用公共API或UI事件以编程方式修改其区域后,将重新计算地图的region
属性。
通过缩小,您可以在地图中心放松精度(一个像素覆盖更大的区域)。然后,在设置具有setRegion:[animated:]
的区域之后,(因此计算的)map.region
可以与setRegion中给出的区域不同。这可以解释你得到的增量。
为了解决您的特定问题,我希望每次用户移动地图时都存储地图的中心,并使用存储的值进行缩放而不是信任map.region.center
。