获取iOS中注释标注的位置
情况如下:我正在使用 MapKit 在地图上显示多个位置。我有代码来计算允许地图上所有点适合窗口的边界。我最近刚刚添加了代码,以允许出现最近位置的标注气泡(正确的术语?)。不幸的是,标注气泡不适合窗口的边界。理想情况下,我希望调整缩放以适应所有注释以及最近位置的标注气泡。关于如何执行此操作有什么建议吗?这是我的缩放方法:
-(void)zoomToFitMapAnnotations:(MKMapView*)mv
{
if([mv.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(WaybackAnnotation *annotation in [mv annotations])
{
if ([annotation isKindOfClass:[WaybackAnnotation class]])
{
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 = [mv regionThatFits:region];
[mv setRegion:region animated:YES];
}
提前致谢! 詹姆斯
here's the situation: I am using MapKit to display a number of locations on a map. I have code to calculate the bounds that allow all the points on the map to fit the window. I just recently added code to allow the closest location's callout bubble (proper term?) to appear. Unfortunately, the callout bubble does not fit in the bounds of the window. Ideally, I'd like the zoom to adjust to fit all the annotations as well as the closest location's callout bubble. Any suggestions on how to do this? Here is my zoom method:
-(void)zoomToFitMapAnnotations:(MKMapView*)mv
{
if([mv.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(WaybackAnnotation *annotation in [mv annotations])
{
if ([annotation isKindOfClass:[WaybackAnnotation class]])
{
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 = [mv regionThatFits:region];
[mv setRegion:region animated:YES];
}
Thanks in advance!
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于气泡始终显示在图钉上方,因此您需要在顶部添加一些额外的填充。您知道填充的高度(以屏幕像素为单位)。使用
MKMapView
的转换方法(-convertRect:toRegionFromView:
等)将它们转换为地理坐标。Since the bubble is always displayed above the pin, you need some extra padding at the top. You know the height of the padding in screen pixels. Use
MKMapView
s conversion methods (-convertRect:toRegionFromView:
and friends) to convert these into geo coordinates.