iPhone 地图区域怪异

发布于 2024-09-17 23:08:48 字数 1786 浏览 10 评论 0原文

alt text

所附图像是当我尝试调整 MKMapView 大小以适合其大小时有时会发生的情况或更多地标。它是间歇性的,但显示屏始终处于完全相同的位置。

这是代码:

 // loc1 is always non-null, and is equal to one of the annotation locations

CLLocationCoordinate2D topLeftCoord = loc1.coordinate;
CLLocationCoordinate2D bottomRightCoord = loc1.coordinate;
for(UserPlacemark* annotation in self.mapView.annotations)
{
    topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);     
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}

MKCoordinateRegion region;
double k = 0.01;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.25;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = k + fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.25; // Add a little extra space on the sides
region.span.longitudeDelta = k + fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.5; // Add a little extra space on the sides

 // only zoom if region doesn't fit, or too small
 CGRect newRect = [mapView convertRegion:region toRectToView:mapView];
 double MIN_SIZE_PIXELS = 50.0;
 double rectSizePixels = newRect.size.width+newRect.size.height;
 if (!CGRectContainsRect(mapView.bounds, newRect) || rectSizePixels < MIN_SIZE_PIXELS)
 {
      region = [mapView regionThatFits:region];
      [mapView setRegion:region animated:TRUE];
 }

alt text

The attached image is what happens sometimes when I try to size a MKMapView to fit one or more placemarks. It's intermittent, but the display is always in exactly the same position.

Here is the code:

 // loc1 is always non-null, and is equal to one of the annotation locations

CLLocationCoordinate2D topLeftCoord = loc1.coordinate;
CLLocationCoordinate2D bottomRightCoord = loc1.coordinate;
for(UserPlacemark* annotation in self.mapView.annotations)
{
    topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);     
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}

MKCoordinateRegion region;
double k = 0.01;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.25;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = k + fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.25; // Add a little extra space on the sides
region.span.longitudeDelta = k + fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.5; // Add a little extra space on the sides

 // only zoom if region doesn't fit, or too small
 CGRect newRect = [mapView convertRegion:region toRectToView:mapView];
 double MIN_SIZE_PIXELS = 50.0;
 double rectSizePixels = newRect.size.width+newRect.size.height;
 if (!CGRectContainsRect(mapView.bounds, newRect) || rectSizePixels < MIN_SIZE_PIXELS)
 {
      region = [mapView regionThatFits:region];
      [mapView setRegion:region animated:TRUE];
 }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-09-24 23:08:48

上面代码中的问题是,Map 框架将 MKUserAnnotation 实例放置在地标列表中。这并不总是与用户的实际位置相对应,至少在模拟器中是这样。

最好避免迭代注释列表,而是直接使用对象计算地标的最小/最大边界。

The problem in the above code is that a MKUserAnnotation instance is placed in the placemark list by the Map framework. This does not always correspond with the user's actual position, at least in the simulator.

It's better to avoid iterating over the annotations list and instead compute the min/max boundaries of your placemark using your objects directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文