- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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
“缩小一点”是行不通的,因为:
MKCoordinateRegionForMapRect
时,它传递的是 annotationMapRect
(单个注释的矩形)而不是 showMapRect
(所有注释的矩形) ).showMapRect
中获取跨度并将其扩展一点(我将其乘以 1.1)。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/
我是一名优秀的程序员,十分优秀!