iOS MKMapView缩放以显示所有标记 [英] iOS MKMapView zoom to show all markers

查看:574
本文介绍了iOS MKMapView缩放以显示所有标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MKMapView 并在地图上绘制了几个点。我已经使用 MKCoordinateRegion MKCoordinateSpan 来启用其中一个点的缩放等 - 但这不是我想要的。 ..

I'm working with MKMapView and have plotted several points on the map. I have used the MKCoordinateRegion and MKCoordinateSpan to enable zooming etc around one of the points - but that's not what I want...

我正在尝试使用类似于Javascript zoom to bounds功能的东西。所以我的所有要点都应该对用户可见。 (英国周围将有大约10个点)我想展示它们,或者如果大多数都在伦敦地区,请放大到那里。

I'm trying to use something similar to the Javascript zoom to bounds function. so all my points should be visible to the user. (There will be around 10 points around the UK) I'd like to show them all, or if most of them were in the London area, zoom to there.

有没有办法以编程方式解决这个问题?

Is there a way to work this out programatically?

推荐答案

当然。您希望在注释中找到最大和最小的纬度和经度值(可以通过迭代map.annotations来完成),然后将地图设置为显示所有注释。

Sure. You want to find the biggest and smallest latitude and longitude values among your annotations (which you can do by iterating over map.annotations), then set the map to show all of them.

// pad our map by 10% around the farthest annotations
#define MAP_PADDING 1.1

// we'll make sure that our minimum vertical span is about a kilometer
// there are ~111km to a degree of latitude. regionThatFits will take care of
// longitude, which is more complicated, anyway. 
#define MINIMUM_VISIBLE_LATITUDE 0.01

MKCoordinateRegion region;
region.center.latitude = (minLatitude + maxLatitude) / 2;
region.center.longitude = (minLongitude + maxLongitude) / 2;

region.span.latitudeDelta = (maxLatitude - minLatitude) * MAP_PADDING;

region.span.latitudeDelta = (region.span.latitudeDelta < MINIMUM_VISIBLE_LATITUDE)
    ? MINIMUM_VISIBLE_LATITUDE 
    : region.span.latitudeDelta;

region.span.longitudeDelta = (maxLongitude - minLongitude) * MAP_PADDING;

MKCoordinateRegion scaledRegion = [map regionThatFits:region];
[map setRegion:scaledRegion animated:YES];

这篇关于iOS MKMapView缩放以显示所有标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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