ios - 如何像 Apple 的 Maps.app 一样在 MKMapView 缩放期间显示 map 比例

标签 ios mkmapview mapkit

我在我的自定义应用程序中使用 MKMapView,并希望在像 Apple 的 Maps.app 一样缩放时显示 map 比例(卷尺)。这可能吗?

如果没有,我会实现我自己的 map 比例尺,当 MKMapView 的缩放比例发生变化时,我如何才能获得连续的更新信息?

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

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

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

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

Maps.app map 比例在缩放期间连续实时显示和更新。

提前致谢。

最佳答案

我有一个类似的问题,让基于用户缩放的 camera.altitude 显示在标签中。

由于没有“regionISChangingAnimated”等方法,只有WillChange和DidChange,所以我在WillChange处启动一个定时器,在DidChange处使它失效。计时器调用一个方法 (updateElevationLabel) 来计算相机在 map 上方的高度。

但是,由于在调用 regionDidChange 之前不会计算 camera.altitude,因此使用 zoomscale 和 map 的起始高度(zoomscale = 1.0 并不总是等于 altitude = 0m,这取决于你在世界的哪个位置)计算当前高度。起始高度在下面的方法中是一个 float ,在加载时和每个区域更改时设置一次。

最后你可以改变高度的格式,例如从 km 到 m 超过一定高度(10'000 米以下)。

对于老派:1 米 = 3.2808399 英尺。

-(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]];
}



}

关于ios - 如何像 Apple 的 Maps.app 一样在 MKMapView 缩放期间显示 map 比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19572377/

相关文章:

iphone - 使用 UIWebView loadRequest 的常规 block 56、1024、8、244、24 内存泄漏

iphone - showUserLocation 在 iPhone 模拟器中返回图钉而不是蓝点

swift - MapView viewForAnnotation 在圆圈中显示白色图钉(默认)

swift - MKPolyLine swift 4

c++ - OpenCV 级联分类器加载错误 iOS

ios - 每秒更新一次 UITableView

ios - 核心数据删除的项目即使在删除后也是可见的(直到应用程序重新启动)

iphone - 过度释放 View 时 EGOTableViewPullRefresh 崩溃

ios - 在跟踪用户位置时保留 map 缩放级别

iphone - CLLocationCooperative2D 到 MKAnnotation