MKMapView を使用するシンプルな iPhone アプリがあります。ズームする地域を調整するセグメント化されたコントロールがあります (通り、近所、都市、州、世界)。問題なく動作しますが、州レベルまでズームアウトして道路に戻ると、中心点がずれていることに気付きます。
以下は、領域の変更の前後に呼び出される 2 つのデリゲート メソッドと、セグメント化されたコントロールによって呼び出される私の changeZoom メソッドです。
- (void)mapView:(MKMapView *)mView regionDidChangeAnimated:(BOOL)animated
{
MKCoordinateRegion 領域 = [mapView 領域];
NSLog(@"リージョン DID が変更されました。中心は現在 %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 領域 = [mapView 領域];
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)送信者
{
UISegmentedControl *sc = (UISegmentedControl *)送信者;
float mile_range = 1;
NSLog(@"changeZoom が呼び出されました。現在の MapView は Center=%f,%f Deltas=%f,%f",
mapView.region.center.latitude、mapView.region.center.longitude、
mapView.region.span.latitudeDelta、mapView.region.span.longitudeDelta );
スイッチ ( [sc selectedSegmentIndex] )
{
case 0: /* ストリート */
mile_range=1;
壊す;
ケース 1: /* フード */
mile_range=2;
壊す;
ケース 2: /* 都市 */
mile_range=30;
壊す;
ケース 3: /* 状態 */
mile_range=500;
壊す;
ケース 4: /* 世界 */
mile_range=4000;
壊す;
デフォルト: /* エラー */
NSLog(@"UISegmentedControl から選択された不明なセグメント!!!!!!!!!!!!!!");
戻る;
壊す;
}
originalZoomCenter = mapView.region.center;
MKCoordinateRegion リージョン。
region = MKCoordinateRegionMakeWithDistance( originalZoomCenter, mile_range/MILES_PER_METER, mile_range/MILES_PER_METER );
region = [mapView regionThatFits: 地域];
NSLog(@"Calling setRegion Center=%f,%f Deltas=%f,%f",
region.center.latitude、region.center.longitude、
region.span.latitudeDelta、region.span.longitudeDelta );
[mapView setRegion: 地域アニメーション: はい];
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 );
}
アプリを実行し、現在の場所のストリート ズーム レベルで開始し、クリックして州レベルにズームアウトし、ストリート レベルに戻ると、コンソールに次の出力が表示されます。
地域は変わります。センターは現在 30.145127、-40.078125、デルタ = 151.851332,225.000000 リージョン DID が変更されました。センターは現在 37.331676、-122.030725、デルタ = 0.024842、0.027466 changeZoom が呼び出されました。MapView は現在、Center=37.331676,-122.030725 Deltas=0.024842,0.027466 です。 呼び出し setRegion Center=37.331676,-122.030725 Deltas=12.708100,14.062500 地域は変わります。センターは現在 37.331676、-122.030725、デルタ = 0.024842、0.027466 setRegion 後 Center=37.331676,-122.030725 Deltas=0.024842,0.027466 リージョン DID が変更されました。センターは現在 37.335224、-122.036133、デルタ = 12.707505、14.062500 changeZoom が呼び出されました。MapView は現在、Center=37.335224,-122.036133 Deltas=12.707505,14.062500 です。 setRegion を呼び出す Center=37.335224,-122.036133 Deltas=0.024841,0.027466 地域は変わります。センターは現在 37.335224、-122.036133、デルタ = 12.707505、14.062500 setRegion Center=37.335224,-122.036133 Deltas=12.707505,14.062500 の後 リージョン DID が変更されました。中心は現在 37.335224、-122.036133、デルタ = 0.024841、0.027466
したがって、コードで行うすべての呼び出しは期待どおりに進み、中心は変化しませんが、MKMapViewDelegate メソッド regionDidChangeAnimated を監視すると、中心が移動することがわかります。そこにブレークポイントを設定すると、次のようなスタックが表示されます。
#0 0x00034c4c in -[MapViewController mapView:regionDidChangeAnimated:] MapViewController.m:209 で #1 -[MKMapView _didChangeRegionMidstream:] の 0x01fbf7bf #2 0x003e11f5 in -[UIView(Gestures) animator:stopAnimation:] #3 0x003743be in -[UIAnimator stopAnimation:] #4 0x00374154 in -[UIAnimator(Static) _advance:] #5 HeartbeatTimerCallback の 0x0264d719 #6 CFRunLoopRunSpecific の 0x01d28ac0 #7 CFRunLoopRunInMode の 0x01d27c48 #8 GSEventRunModal の 0x0264a78d #9 GSEventRun の 0x0264a852 #10 UIApplicationMain の 0x00307003 #11 0x00002194 メインの main.m:14
これは、センターが移動する理由を理解するのに本当に役立ちません。これをシミュレーターとデバイスの両方で実行しましたが、少し頭がおかしくなりました。
中心がずれている原因について誰かが何か洞察を持っているなら、私は何かの指針をいただければ幸いです.