1

我正在尝试在 MKMapView 上绘制多个点。在两个直线之间,这很容易——我只需找到中间坐标并使用它们:

// Center
CLLocationCoordinate2D center;
center.latitude = self.midLatitude;
center.longitude = self.midLongitude;
myRegion.center = center;

但是,如果地图上有洛杉矶、华盛顿特区和西雅图呢?我有自己的代码,它根据数组中的第一个和最后一个坐标计算中心。在上述情况下,我只看到洛杉矶和西雅图。

有没有另一种方法来确定三点之间的中心?

谢谢!

编辑:

添加了 MKMapRect 的新代码:

-(void) addAnnotation {

MKMapRect showMapRect = MKMapRectNull;
CLLocationCoordinate2D mapLocation;
IGAMapAnnotation *mapAnnotation;

// Calculate how many points are included in the plan
NSInteger numberOfPoints = [coordinatesTempArray count];

MKMapPoint annotationMapPoint;
MKMapRect annotationMapRect;

if (numberOfPoints > 0) {
    // Trying to add coordinates from the array of coordinates
    for (NSInteger i=0; i < ([coordinatesTempArray count]); i++) {

        ... // Code that adds annotations

        // Adding the annotation to the array that will be added to the map
        [mapLocations addObject:mapAnnotation];

        // Adding Annotation coordinates to MKMapRect so that they all be visible in the view
        annotationMapPoint = MKMapPointForCoordinate(mapLocation);
        annotationMapRect = MKMapRectMake(annotationMapPoint.x, annotationMapPoint.y, 0, 0);

        showMapRect = MKMapRectUnion(showMapRect, annotationMapRect);
        }       
    }

    // Showing all annotations at a time
    self.mapView.visibleMapRect = showMapRect;

    // Now I am trying to zoom out a bit since the extreme annotation are right at the border of the mapview. THIS DOES NOT WORK.

    MKCoordinateRegion region;
   region = MKCoordinateRegionForMapRect(annotationMapRect);

    MKCoordinateSpan span;
    span.latitudeDelta = 0.09;
    span.longitudeDelta = 0.09;
    region.span = span;
    region = [self.mapView regionThatFits:region];
    [self.mapView setRegion:region animated:YES];


    // Showing all annotations at a time
    self.mapView.visibleMapRect = showMapRect;


    // Adding annotations to the map
    [self.mapView addAnnotations:mapLocations];

    }
 }
4

1 回答 1

1

在编辑后的代码中,尝试“缩小一点”而不是计算结果MKMapRect不起作用,因为:

  1. 调用时MKCoordinateRegionForMapRect,它传递的是annotationMapRect(单个注释的矩形)而不是showMapRect(所有注释的矩形)。
  2. 跨度增量设置为固定值,而不是从中获取跨度showMapRect并稍微扩展它(我将其乘以 1.1)。
  3. setRegion使用“缩小区域”调用后,代码再次setVisibileMapRect使用原始代码调用,showMapRect这将撤消缩小区域的所有工作。

修复上述问题后,您可以使用缩小一点setRegion,但您已经发现的更简单的方法是使用setVisibleMapRect:edgePadding:animated:而不是setVisibleMapRect:.


关于问题mapViewDidFailLoadingMap

很难说为什么地图在您的特定情况下无法加载。

但无论原因是什么,如果您必须让用户知道地图无法加载,我的建议是使用 aUILabel而不是 a UIAlertView

与其让用户在每次地图视图加载出现问题时点击确定,不如在地图视图失败/完成加载时显示/隐藏标签。

通过这种方式,用户会得到“提醒”,但他们不会因为不得不不断点击 OK 而感到恼火。

这可能会给用户带来更愉悦的体验。

使标签成为地图视图是其子视图的同一视图的子视图。也就是说,让他们成为兄弟姐妹。不要使标签成为地图视图的子视图。最初将标签设置为“隐藏”或alpha设置为 0,以便您可以在委托方法中为标签显示/隐藏设置动画。您可以将标签放在地图视图的前面(如果屏幕上没有空间),但不能放在地图视图的子视图前面。

例子:

//initialize label alpha to 0 in viewDidLoad...
mapLoadFailedLabel.alpha = 0;

-(void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
    [UIView animateWithDuration:0.5 animations:^{
        mapLoadFailedLabel.alpha = 1.0;
    }];
}

-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
    [UIView animateWithDuration:0.5 animations:^{
        mapLoadFailedLabel.alpha = 0.0;
    }];
}
于 2014-08-12T02:48:45.857 回答