iPhone MKMapView:设置跨度/区域值以在地图上显示所有图钉 [英] iPhone MKMapView: set span/region value to show all pins on map

查看:127
本文介绍了iPhone MKMapView:设置跨度/区域值以在地图上显示所有图钉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目(iOS 7 平台),其中我需要大约 5 公里的商店的当前位置,因此如何计算跨度/区域值以在地图上显示所有具有当前位置的商店.

I'm working on a project (platform iOS 7) in which i required current location with stores around 5km, so how to calculate the span/region value to display all stores with current location on map.

MKMapRect zoomRect = MKMapRectNull; 
double inset; 
for (id <MKAnnotation> annotation in mapVW.annotations) 
{
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1); 
    zoomRect = MKMapRectUnion(zoomRect, pointRect); 
    inset = -zoomRect.size.width * 20; 
} 
[mapVW setVisibleMapRect:MKMapRectInset(zoomRect, inset, inset) animated:YES]; 

这就是我正在尝试的

谢谢

推荐答案

目前尚不清楚您的确切问题是什么,但以下内容可能会有所帮助:

It's not clear what your exact issue is but the following may help:

  • inset 的计算看起来不对.它将inset(边上的填充)设置为整个缩放区域宽度的20 倍.您可能想要的是将 inset 设置为整个宽度的一小部分 fraction.也许你的意思是 0.20 而不是 20.0:

  • The calculation of the inset looks wrong. It's setting the inset (padding on the sides) to 20 times the width of the whole zoom area. What you probably want is to set the inset to a small fraction of the entire width. Maybe you meant 0.20 instead of 20.0:

inset = -zoomRect.size.width * 0.20;

您也不需要在 for 循环中重复设置 inset,因为它只取决于最终的 width.在调用 setVisibleMapRect 之前,您可以在 for 循环之后移动上面的行.

You also don't need to repeatedly set the inset inside the for loop since it only depends on the final width. You can move the above line after the for loop before calling setVisibleMapRect.


顺便说一句:iOS 7 包含新方法 showAnnotations:animated:,它会自动确定某些给定注释的边界矩形并为您设置地图的可见区域.它不允许您像正在做的那样指定自定义插图(尽管默认设置还不错).因此,您可以执行以下操作,而不是上述循环:


By the way: iOS 7 includes the new method showAnnotations:animated: which automatically determines the bounding rectangle for some given annotations and sets the map's visible region for you. It doesn't let you specify a custom inset like you are doing (though the default isn't bad). So instead of the above loop, you would do:

[mapVW showAnnotations:mapVW.annotations animated:YES];

这篇关于iPhone MKMapView:设置跨度/区域值以在地图上显示所有图钉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆