Ios 如何在像Apple';s Maps.app
我在我的自定义应用程序中使用MKMapView,并希望在缩放过程中显示地图比例(卷尺),就像苹果的Maps.app一样。这可能吗 如果没有,我将实现我自己的地图比例,我如何在更改MKMapView的缩放比例时获得连续更新信息Ios 如何在像Apple';s Maps.app,ios,mkmapview,mapkit,Ios,Mkmapview,Mapkit,我在我的自定义应用程序中使用MKMapView,并希望在缩放过程中显示地图比例(卷尺),就像苹果的Maps.app一样。这可能吗 如果没有,我将实现我自己的地图比例,我如何在更改MKMapView的缩放比例时获得连续更新信息 - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated 似乎在缩放开始时只调用一次 - (void)mapView:(MKMapView *)mapView regionDi
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
似乎在缩放开始时只调用一次
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
仅在缩放结束时调用一次
Maps.app地图比例在缩放过程中连续实时显示和更新
提前感谢。来自苹果在MKMapView区域的文档willChangeAnimated:method(emphasis mine): 只要当前显示的地图区域 变化。在滚动期间,可能会多次调用此方法以 报告地图位置的更新。因此,您实施 此方法应尽可能轻量级,以避免影响 滚动性能
听起来您应该能够在地图视图滚动时连续使用此方法,这解决了部分问题-因此,当调用此方法时,请查看(我在这里猜测)地图视图的
区域属性,并从中导出地图比例。我也有类似的问题,基于用户缩放获取camera.altitude以显示在标签中
因为没有“regionischanganimated”这样的方法,只有WillChange和DidChange,所以我在WillChange启动一个计时器,并在DidChange使其无效。计时器调用一个方法(UpdateLevationLabel),该方法计算相机在地图上方的高度
但是,由于在调用regionDidChange之前不会计算camera.altitude,因此请使用zoomscale和地图的起始高度(zoomscale=1.0并不总是等于altitude=0m,这取决于您在世界上的位置)来计算当前高度。在下面的方法中,起始高度是一个浮动,在加载和每个区域更改时设置一次
最后,您可以更改高度的格式,例如,从千米到米,超过某一高度(低于10000米)
对于旧学校: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]];
}
}
与文档所说的相反,这个方法不是连续调用的,而是在原始帖子中所述的缩放开始时调用一次。