我有一张正确显示的地图,我现在唯一要做的就是在加载时设置缩放级别.有没有办法做到这一点?
谢谢
我发现自己是一个解决方案,这很简单并且可以解决问题.使用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;
迅速:
let location = ... let region = MKCoordinateRegionMakeWithDistance(location.coordinate, CLLocationDistance(exactly: 5000)!, CLLocationDistance(exactly: 5000)!) mapView.setRegion(mapView.regionThatFits(region), animated: true)
基于经度线在地图的任何点均匀分布的事实,有一个非常简单的实现来设置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];
快速实施
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 = MKCoordinateSpan(latitudeDelta: 0, longitudeDelta: 360 / pow(2, Double(zoomLevel)) * Double(self.frame.size.width) / 256) setRegion(MKCoordinateRegion(center: coordinate, span: span), animated: animated) } }
一个简单的Swift实现,如果你使用outlet.
@IBOutlet weak var mapView: MKMapView! { didSet { let noLocation = CLLocationCoordinate2D() let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500) self.mapView.setRegion(viewRegion, animated: false) } }
根据@ Carnal的回答.
对于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
,mapView就会使缩放级别符合您的值.
基于@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