我是否必须在我的MKMapview委托上实现mapView:regionWillChangeAnimated:

时间:2011-01-30 01:44:19

标签: iphone objective-c mkmapview

Apple的文档告诉你这个方法应该尽可能轻量级,这里有什么标准用法?重置注释引脚?

  

告诉代表该地区   由地图视图显示即将到来   变化

     

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

     

参数

     

MapView的
  可见区域的地图视图   即将改变。

     

动画
  如果是,则更改为新地区   将动画。如果否,则改变   将立即制作。

     

每当调用此方法时   目前显示的地图区域   变化。在滚动期间,此方法   可能会被多次召集报告   更新地图位置。   因此,您执行此操作   方法应该像轻量级一样   可以避免影响滚动   性能

1 个答案:

答案 0 :(得分:0)

此委托方法的问题是“在滚动期间,可能会多次调用此方法以报告对地图位置的更新”(因此您需要IF / THEN或CASE / BREAK等以使其“轻量级”)。

你根本不需要使用这种方法(不是必需的),但如果你确实希望包含某种功能(例如删除无用的引脚等),那么保持轻量级的示例代码将是:

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
if(!animated){
//Instantaneous change, which means you probably did something code-wise, so you should have handled anything there, but you can do it here as well.

} else {
//User is most likely scrolling, so the best way to do things here is check if the new region is significantly (by whatever standard) away from the starting region

CLLocationDistance *distance = [mapView.centerCoordinate distanceFromLocation:originalCoordinate];
if(distance > 1000){
//The map region was shifted by 1000 meters
//Remove annotations outsides the view, or whatever
//Most likely, instead of checking for a distance change, you might want to check for a change relative to the view size
}

}

}