gpt4 book ai didi

ios - MK map View 。寻找具有三个散点的区域

转载 作者:行者123 更新时间:2023-11-29 12:39:59 25 4
gpt4 key购买 nike

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

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

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

还有其他方法可以确定三点之间的中心吗?

谢谢!

编辑:

添加了 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];

}
}

最佳答案

在编辑后的代码中,尝试比计算的MKMapRect“缩小一点”是行不通的,因为:

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

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


关于mapViewDidFailLoadingMap的问题:

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

但是无论是什么原因,如果您必须让用户知道 map 加载失败,我的建议是使用 UILabel 而不是 UIAlertView

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

这样,用户会收到“提醒”,但他们不会因为不断点击“确定”而烦恼。

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

使标签成为 map View 是其 subview 的同一 View 的 subview 。也就是说,让他们成为 sibling 。不要使标签成为 map View 的 subview 。最初将标签设置为“隐藏”或将 alpha 设置为 0,这样您就可以在委托(delegate)方法中为标签显示/隐藏设置动画。您可以将标签放在 map View 前面(如果屏幕上没有空间),但不能放在 map View 的 subview 上。

例子:

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

关于ios - MK map View 。寻找具有三个散点的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25235042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com