如何在缩放MKMapView期间显示地图比例,如Apple的Maps.app

时间:2013-10-24 17:15:08

标签: ios mkmapview mapkit

我在我的自定义应用程序中使用MKMapView,并希望在缩放期间显示地图比例(卷尺),如Apple的Maps.app。这可能吗?

如果没有,我将实现自己的地图比例,如何在MKMapView的缩放时更改连续的更新信息?

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated

似乎只在缩放开始时调用一次

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

在缩放结束时仅调用一次。

Maps.app地图比例在缩放过程中连续显示和更新。

提前感谢。

2 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,根据用户缩放获取camera.altitude,以便在标签中显示。

由于没有像“regionISChangingAnimated”那样的方法,只有WillChange和DidChange,我在WillChange启动一个计时器并在DidChange中使它无效。计时器调用一个方法(updateElevationLabel)来计算地图上方摄像机的高度。

但是,由于在调用regionDidChange之前不计算camera.altitude,因此请使用缩放比例和地图的起始高度(zoomscale = 1.0并不总是等于海拔= 0m,这取决于您在世界上的位置)计算当前的高度。起始高度在下面的方法中是浮点数,在加载时设置一次,并且每个区域更改一次。

最后,您可以更改海拔高度的格式,例如从m以上超过一定高度(下方10'000米)。

对于oldschool:1m = 3.2808399 ft。

-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {

    if (showsElevation) {

    //update starting height for the region
    MKMapCamera *camera = map.camera;
    CLLocationDistance altitude = camera.altitude;
    MKZoomScale currentZoomScale = map.bounds.size.width / map.visibleMapRect.size.width;
    float factor = 1.0/currentZoomScale;
    startingHeight = altitude/factor;

    elevationTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                      target:self
                                                    selector:@selector(updateElevationLabel)
                                                    userInfo:Nil
                                                     repeats:YES];
    [elevationTimer fire];

    }
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    [elevationTimer invalidate];
}


-(void)updateElevationLabel {


//1. create the label
if (!elevationLabel) {
    elevationLabel = [UILabel new];
    [elevationLabel setFrame:CGRectMake(0, 18, 200, 44)];
    [elevationLabel setBackgroundColor:[UIColor redColor]];
    [self addSubview:elevationLabel];
}

//2. grab the initial starting height (further updated on region changes)
if (startingHeight == 0) {
    MKMapCamera *camera = map.camera;
    CLLocationDistance altitude = camera.altitude;
    MKZoomScale currentZoomScale = map.bounds.size.width / map.visibleMapRect.size.width;
    float factor = 1.0/currentZoomScale;
    startingHeight = altitude/factor;
}

//3. get current zoom scale and altitude, format changes
MKZoomScale currentZoomScale = map.bounds.size.width / map.visibleMapRect.size.width;
float altitude = startingHeight * (1/currentZoomScale);
if (altitude>10000) {
    altitude = altitude/1000;
        [elevationLabel setText:[NSString stringWithFormat:@"%.1fkm", altitude]];
} else {
        [elevationLabel setText:[NSString stringWithFormat:@"%.0fm", altitude]];
}



}

答案 1 :(得分:0)

来自Apple的关于MKMapView& RegionWillChangeAnimated:方法的文档(强调我的):

  

只要当前显示的地图区域,就会调用此方法   变化。在滚动期间,可以多次调用此方法   报告地图位置的更新。因此,您的实施   这种方法应尽量轻巧,以免影响   滚动表现。

听起来你应该能够在地图视图滚动时连续使用这个方法,这可以解决部分问题 - 所以当调用该方法时,请看一下(我在这里猜测)mapview&# 39; s region属性并从中获取地图比例。