如果我在地图视图中有3个引脚,当地图加载时如何缩放到这些引脚。也就是说,当地图加载时,我需要放大视图,但视图应该适应地图中放置的所有针脚。它的工作,当地图只有一个引脚。但我无法处理多个引脚的缩放。帮助需要将地图视图缩放到引脚掉落的区域
1
A
回答
4
我写的下面的代码在应用程序中,它类似于@ElanthiraiyanS回答
-(void)zoomToFitMapAnnotations:(MKMapView*)mapView insideArray:(NSArray*)anAnnotationArray
{
// NSLog(@"%s", __FUNCTION__);
if([mapView.annotations count] == 0) return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MKPointAnnotation* annotation in anAnnotationArray)
{
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;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
0
您可以calc下所有引脚的中心
- (CLLocationCoordinate2D)centerPointFromAnnotationArray:(NSArray *)array
{
CLLocationCoordinate2D coorMax = CLLocationCoordinate2DMake(0, 0);
CLLocationCoordinate2D coorMin = CLLocationCoordinate2DMake(999, 999);
for (int i = 0 ; i < [array count] ; ++i)
{
LMPoint *a = [array objectAtIndex:i];
if ( (a.coordinate.latitude != 0.0f) || (a.coordinate.longitude != 0.0f))
{
if (a.coordinate.latitude > coorMax.latitude)
{
coorMax.latitude = a.coordinate.latitude;
}
if (a.coordinate.longitude > coorMax.longitude)
{
coorMax.longitude = a.coordinate.longitude;
}
if (a.coordinate.latitude < coorMin.latitude)
{
coorMin.latitude = a.coordinate.latitude;
}
if (a.coordinate.longitude < coorMin.longitude)
{
coorMin.longitude = a.coordinate.longitude;
}
}
}
CLLocationCoordinate2D coorCenter = CLLocationCoordinate2DMake((coorMax.latitude+coorMin.latitude)/2, (coorMax.longitude+coorMin.longitude)/2);
return coorCenter;
}
然后在点放大以适当的缩放级别
6
下面的方法做,
- (void)zoomToFitMapAnnotations:(MKMapView *)mapView {
if ([mapView.annotations count] == 0) return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MapAnnotation *annotation in 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;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
// Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
// Add a little extra space on the sides
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
+0
+1真的对我很有帮助。我刚刚更改了这一行,以便让更多空间精确显示'region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude)* 2.0;' – Praveenkumar 2013-03-13 14:27:55
相关问题
- 1. 目标C:掉落区域
- 2. 谷歌地图v3 - 限制可视区域和缩放级别
- 3. 拉伸/缩放视图的特定区域/位图
- 4. 缩放地图/带CSS的区域,移动网络
- 5. 将视图缩放到中心子视图
- 6. 在图像视图中按下缩略图区域时,图像不会缩小
- 7. Android缩放视图
- 8. 地图不缩放
- 9. JS地图区域
- 10. 极地区域图
- 11. 缩放地图以在整个显示区域中显示覆盖图
- 12. 试图将中心地图放在引脚上(MKMapView)
- 13. 图线不随可缩放图表区域移动
- 14. 不允许子视图缩放视图?
- 15. 如何将缩放控件添加到地图图像
- 16. 重新缩放位图的缩放视图
- 17. jquery缩放图像与图像地图
- 18. 在UIScrollview上缩放图像后,它滚动到图像区域外
- 19. 启用UIScrollView缩放重置缩放视图转换的缩放
- 20. 缩放绘图像flex中的地图
- 21. 将css应用于区域地图
- 22. 如何将地图视图中的针脚发回到呼出视图?
- 23. 谷歌地图上的绘图区域
- 24. 我们如何制作地图视图区域的数组?
- 25. UIView缩放到指纹,子视图呢?
- 26. MultiScaleImage缩放特定区域
- 27. 如何将从SD卡加载的图像缩放到图像视图?
- 28. 棱镜,映射区域到视图
- 29. 得到可视图像区域
- 30. WPF视图框缩放
什么是注释阵列。它的内容是什么? – 2012-04-19 09:28:00
@JeanPaulScott针脚数组(MKPointAnnotation) – 2012-04-19 09:32:55
很好地完成。谢谢! – CocoaEv 2012-08-23 05:04:08