MKMapView 跨度加倍错误

发布于 2024-08-30 19:24:48 字数 1470 浏览 5 评论 0原文

在 MKMapView 中设置区域有时会导致跨度加倍。这个错误似乎出现在地图初始化阶段的早期。尽管在其他地方有报道,但我无法找到下降的现有解决方法,因此我在这里发布我的修复程序。它依赖于以下事实:regionThatFits 方法也会产生错误。我正在使用 iPhone OS 3.12,但该错误已在 3.0 beta 中报告。此代码位于包含 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];
  }
}

Setting the region in MKMapView occasionally results in the span being doubled. This bug seems to appear early in the map initialization phase. Although it's been reported elsewhere I wasn't able to find a descent existing workaround, so I'm posting my fix here. It relies on the fact that the regionThatFits method also produces the bug. I'm working with iPhone OS 3.12, but the bug was reported in 3.0 beta. This code lives in the UIViewController that contains your MKMapView:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文