我有一个正确显示的地图,我现在唯一想做的就是设置加载时的缩放级别。有没有办法做到这一点?
谢谢
发布于 2012-07-17 17:35:34
我给自己找到了一个解决方案,这个方案很简单,而且很管用。使用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;
Swift:
let location = ...
let region = MKCoordinateRegion( center: location.coordinate, latitudinalMeters: CLLocationDistance(exactly: 5000)!, longitudinalMeters: CLLocationDistance(exactly: 5000)!)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
发布于 2013-02-22 17:14:05
基于经度线在地图上的任意点等距分布的事实,有一个非常简单的实现来设置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
发布于 2010-11-16 06:49:05
它不是内置的,但我见过/用过this代码。这使您可以使用以下内容:
[mapView setCenterCoordinate:myCoord zoomLevel:13 animated:YES];
注意:这不是我的代码,不是我写的,因此不能归功于它
https://stackoverflow.com/questions/4189621
复制相似问题