Riz*_*zon 23 iphone objective-c mapkit iphone-sdk-3.0 mkmapview
我在iPhone上使用MapKit.如何知道用户何时更改缩放级别(放大\地图)?
我试过用mapView:(MKMapView*)mapView regionDidChangeAnimated:(BOOL)动画; 但即使只拖动地图也会调用它.不幸的是,当拖动地图时,mapView.region.span也会发生变化......
救命?
10倍
afa*_*ham 37
计算缩放级别非常简单.请参阅下面的代码段.您可以从实例的visibleMapRect属性中获取mRect参数MKMapView.
+ (NSUInteger)zoomLevelForMapRect:(MKMapRect)mRect withMapViewSizeInPixels:(CGSize)viewSizeInPixels
{
NSUInteger zoomLevel = MAXIMUM_ZOOM; // MAXIMUM_ZOOM is 20 with MapKit
MKZoomScale zoomScale = mRect.size.width / viewSizeInPixels.width; //MKZoomScale is just a CGFloat typedef
double zoomExponent = log2(zoomScale);
zoomLevel = (NSUInteger)(MAXIMUM_ZOOM - ceil(zoomExponent));
return zoomLevel;
}
Run Code Online (Sandbox Code Playgroud)
你可能只是停在计算的步骤zoomScale,它将告诉你缩放是否完全改变了.
我通过阅读Troy Brants关于该主题的优秀博客文章来想出这些内容:
http://troybrant.net/blog/2010/01/mkmapview-and-zoom-levels-a-visual-guide/
斯威夫特3
extension MKMapView {
var zoomLevel: Int {
let maxZoom: Double = 20
let zoomScale = self.visibleMapRect.size.width / Double(self.frame.size.width)
let zoomExponent = log2(zoomScale)
return Int(maxZoom - ceil(zoomExponent))
}
}
Run Code Online (Sandbox Code Playgroud)
小智 11
我发现这非常有用,并根据这些答案开发了以下代码.
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
mapRegion = self.mapView.region;
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
MKCoordinateRegion newRegion = self.mapView.region;
NSInteger zFactor;
if ((mapRegion.span.latitudeDelta/newRegion.span.latitudeDelta) > 1.5){
NSLog(@"Zoom in changed");
zFactor = 20;
CustomPlacemark *aO;
MKAnnotationView *aV;
for (aO in self.mapView.annotations) {
aV = [[self mapView] viewForAnnotation:aO];
aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y, aV.frame.size.width+zFactor, aV.frame.size.height+zFactor);
[[[self mapView] viewForAnnotation:aO] setFrame:aV.frame];
}
}
if ((mapRegion.span.latitudeDelta/newRegion.span.latitudeDelta) < 0.75){
NSLog(@"Zoom out");
zFactor = -20;
CustomPlacemark *aO;
MKAnnotationView *aV;
for (aO in self.mapView.annotations) {
aV = [[self mapView] viewForAnnotation:aO];
aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y, aV.frame.size.width+zFactor, aV.frame.size.height+zFactor);
[[[self mapView] viewForAnnotation:aO] setFrame:aV.frame];
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16283 次 |
| 最近记录: |