设置MKMapView的缩放级别

我有一个正确显示的地图,我现在要做的唯一事情是在加载时设置缩放级别。 有没有办法做到这一点?

谢谢

我发现自己是一个解决scheme,这是非常简单的,并伎俩。 使用MKCoordinateRegionMakeWithDistance为了设置垂直和水平米的距离,以获得所需的缩放。 然后当然,当你更新你的位置时,你会得到正确的坐标,或者你可以在启动时直接在CLLocationCoordinate2D中指定它,如果这是你需要做的:

 CLLocationCoordinate2D noLocation; MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500); MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion]; [self.mapView setRegion:adjustedRegion animated:YES]; self.mapView.showsUserLocation = YES; 

基于经度线在地图上的任何点均等分开的事实,设置centerCoordinate和zoomLevel有一个非常简单的实现:

 @interface MKMapView (ZoomLevel) @property (assign, nonatomic) NSUInteger zoomLevel; - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated; @end @implementation MKMapView (ZoomLevel) - (void)setZoomLevel:(NSUInteger)zoomLevel { [self setCenterCoordinate:self.centerCoordinate zoomLevel:zoomLevel animated:NO]; } - (NSUInteger)zoomLevel { return log2(360 * ((self.frame.size.width/256) / self.region.span.longitudeDelta)) + 1; } - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated { MKCoordinateSpan span = MKCoordinateSpanMake(0, 360/pow(2, zoomLevel)*self.frame.size.width/256); [self setRegion:MKCoordinateRegionMake(centerCoordinate, span) animated:animated]; } @end 

这不是内置的,但我已经看到/使用这个代码。 这允许你使用这个:

 [mapView setCenterCoordinate:myCoord zoomLevel:13 animated:YES]; 

注:这不是我的代码,我没有写,所以不能赞扬它

您也可以通过使用MKCoordinateRegion并设置其跨度经度增量来缩放。 下面是一个快速参考, 这里是iOS的参考。 它不会做任何幻想,但应该允许您在绘制地图时设置缩放。


 MKCoordinateRegion region; region.center.latitude = {desired lat}; region.center.longitude = {desired lng}; region.span.latitudeDelta = 1; region.span.longitudeDelta = 1; mapView.region = region; 

编辑1:

 MKCoordinateRegion region; region.center.latitude = {desired lat}; region.center.longitude = {desired lng}; region.span.latitudeDelta = 1; region.span.longitudeDelta = 1; region = [mapView regionThatFits:region]; [mapView setRegion:region animated:TRUE]; 

一个简单的Swift实现,如果你使用sockets。

 @IBOutlet weak var mapView: MKMapView! { didSet { let noLocation = CLLocationCoordinate2D() let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500) self.mapView.setRegion(viewRegion, animated: false) } } 

根据@ Carnal的回答。

基于@ AdilSoomro的伟大答案 。 我已经想出了这个:

 @interface MKMapView (ZoomLevel) - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated; -(double) getZoomLevel; @end @implementation MKMapView (ZoomLevel) - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated { MKCoordinateSpan span = MKCoordinateSpanMake(0, 360/pow(2, zoomLevel)*self.frame.size.width/256); [self setRegion:MKCoordinateRegionMake(centerCoordinate, span) animated:animated]; } -(double) getZoomLevel { return log2(360 * ((self.frame.size.width/256) / self.region.span.longitudeDelta)); } @end 

对于Swift 3来说,这是非常快速的:

 private func setMapRegion(for location: CLLocationCoordinate2D, animated: Bool) { let viewRegion = MKCoordinateRegionMakeWithDistance(location, <#T##latitudinalMeters: CLLocationDistance##CLLocationDistance#>, <#T##longitudinalMeters: CLLocationDistance##CLLocationDistance#>) MapView.setRegion(viewRegion, animated: animated) } 

只需定义lat-,long-Meters <CLLocationDistance> ,mapView就会将缩放级别与您的值匹配。

Swift实现

 import Foundation import MapKit class MapViewWithZoom: MKMapView { var zoomLevel: Int { get { return Int(log2(360 * (Double(self.frame.size.width/256) / self.region.span.longitudeDelta)) + 1); } set (newZoomLevel){ setCenterCoordinate(coordinate:self.centerCoordinate, zoomLevel: newZoomLevel, animated: false) } } private func setCenterCoordinate(coordinate: CLLocationCoordinate2D, zoomLevel: Int, animated: Bool){ let span = MKCoordinateSpanMake(0, 360 / pow(2, Double(zoomLevel)) * Double(self.frame.size.width) / 256) setRegion(MKCoordinateRegionMake(coordinate, span), animated: animated) } } 

我希望下面的代码片段可以帮助你。

 - (void)handleZoomOutAction:(id)sender { MKCoordinateRegion newRegion=MKCoordinateRegionMake(mapView.region.center,MKCoordinateSpanMake(mapView.region.s pan.latitudeDelta/0.5, mapView.region.span.longitudeDelta/0.5)); [mapView setRegion:newRegion]; } - (void)handleZoomInAction:(id)sender { MKCoordinateRegion newRegion=MKCoordinateRegionMake(mapView.region.center,MKCoordinateSpanMake(mapView.region.span.latitudeDelta*0.5, mapView.region.span.longitudeDelta*0.5)); [mapView setRegion:newRegion]; } 

您可以select任何值而不是0.5来减小或增加缩放级别。 我已经使用这些方法点击两个button。

我知道这是一个迟到的回复,但我只想解决自己设置缩放级别的问题。 goldmine的答案很好,但是我发现在我的应用程序中它不能很好地工作。

仔细观察,金矿称“经线在地图的任何一点上都是等距的”。 这是不正确的,实际上纬线从-90(南极)到+90(北极)是等距的。 经线在赤道处最宽,与两极汇合。

我所采用的实施方式是使用纬度计算如下:

 @implementation MKMapView (ZoomLevel) - (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate zoomLevel:(NSUInteger)zoom animated:(BOOL)animated { MKCoordinateSpan span = MKCoordinateSpanMake(180 / pow(2, zoom) * self.frame.size.height / 256, 0); [self setRegion:MKCoordinateRegionMake(coordinate, span) animated:animated]; } @end 

希望在这个晚期有所帮助。

一个Swift 2.0答案利用NSUserDefaults来保存和恢复地图的缩放和位置。

保存地图位置和缩放的function:

 func saveMapRegion() { let mapRegion = [ "latitude" : mapView.region.center.latitude, "longitude" : mapView.region.center.longitude, "latitudeDelta" : mapView.region.span.latitudeDelta, "longitudeDelta" : mapView.region.span.longitudeDelta ] NSUserDefaults.standardUserDefaults().setObject(mapRegion, forKey: "mapRegion") } 

每次移动地图时运行函数:

 func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) { saveMapRegion(); } 

保存地图的function缩放和位置:

 func restoreMapRegion() { if let mapRegion = NSUserDefaults.standardUserDefaults().objectForKey("mapRegion") { let longitude = mapRegion["longitude"] as! CLLocationDegrees let latitude = mapRegion["latitude"] as! CLLocationDegrees let center = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) let longitudeDelta = mapRegion["latitudeDelta"] as! CLLocationDegrees let latitudeDelta = mapRegion["longitudeDelta"] as! CLLocationDegrees let span = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta) let savedRegion = MKCoordinateRegion(center: center, span: span) self.mapView.setRegion(savedRegion, animated: false) } } 

将此添加到viewDidLoad:

 restoreMapRegion()