2

MKMapViewでリージョンを設定すると、スパンが2倍になることがあります。このバグは、マップの初期化フェーズの早い段階で発生するようです。他の場所で報告されていますが、既存の回避策を見つけることができなかったので、ここに修正を投稿します。これは、regionThatFitsメソッドもバグを生成するという事実に依存しています。私はiPhoneOS3.12を使用していますが、バグは3.0ベータ版で報告されました。このコードは、MKMapViewを含むUIViewControllerにあります。

    - (BOOL)doubleSpanBugDetected:(MKCoordinateRegion)region fittedRegion:(MKCoordinateRegion)fitted
{
  float latRatio = fitted.span.latitudeDelta / region.span.latitudeDelta;
  float lonRatio = fitted.span.longitudeDelta / region.span.longitudeDelta;
  BOOL latDoubled = (latRatio > 1.8 && latRatio < 2.2); // within 10% of x2
  BOOL lonDoubled = (lonRatio > 1.8 && lonRatio < 2.2); // within 10% of x2
  return latDoubled && lonDoubled;
}

- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated
{
  //fixes setRegion span doubling bug
  // see: http://osmorphis.blogspot.com/2009/12/mapkit-span-doubling-bug.html
  // see: http://www.iphonedevsdk.com/forum/iphone-sdk-development/15810-mkmapview-needs-time-think.html
  MKCoordinateRegion fitted = [self.mapView regionThatFits:region];
  if ([self doubleSpanBugDetected:region fittedRegion:fitted]) {
    MKCoordinateSpan span = MKCoordinateSpanMake(fitted.span.latitudeDelta/2.0, fitted.span.longitudeDelta/2.0);
    MKCoordinateRegion regionHack = MKCoordinateRegionMake(fitted.center, span); 
    [self.mapView setRegion:regionHack animated:animated];     
  } else {
    [self.mapView setRegion:fitted animated:animated];
  }
}
4

0 に答える 0