您的位置:首页 > 移动开发 > IOS开发

ArcGIS for iOS 开发系列(5) – 基础篇-手势和事件响应

2012-10-11 09:32 579 查看
    MapView默认支持以下手势操作及对应触发的通知:

表3-2-1 支持的手势及对应通知

手势
地图响应
触发的通知
轻划(Swipe)
平移
MapDidEndPanning
撑开(Pinch-Out)
放大
MapDidEndZooming
捏合(Pinch-In)
缩小
MapDidEndZooming
双击(Double Tap)
放大
MapDidEndZooming
两点点击(Two finger Tap)
缩小
MapDidEndZooming
两点转动(Two finger Twist)*
旋转
 
*地图旋转前提是MapView的allowRotationByPinching属性为YES。

1.地图通知

    地图平移和缩放动作的通知非常有用,帮助开发者对地图范围变化做出响应,其调用方法如下:

在viewDidLoad方法中先注册“MapDidEndPanning”和“MapDidEndZooming”这两个通知。

    // register for "MapDidEndPanning"notifications

    [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(respondToEnvChange:)name:@"MapDidEndPanning"object:nil];

    // register for "MapDidEndZooming"notifications

    [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(respondToEnvChange:)name:@"MapDidEndZooming"object:nil];

 

    然后创建一个“respondToEnvChange”方法接收通知,如下:

// The method that should be called when thenotification arises

- (void)respondToEnvChange:(NSNotification*) notification {

    //create the string containing the new mapextent NSString*

    NSString* theString = [[NSStringalloc]initWithFormat:@"xmin
= %f,\nymin = %f,\nxmax = %f,\nymax= %f",

                           self.mapView.visibleArea.envelope.xmin,

                           self.mapView.visibleArea.envelope.ymin,

                           self.mapView.visibleArea.envelope.xmax,

                           self.mapView.visibleArea.envelope.ymax];

    //display the new map extent in a simple alert

    UIAlertView* alertView = [[UIAlertViewalloc]    initWithTitle:@"mapViewDidEndPanning"

                                                        message:theStringdelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];

   [alertView show];

}

    此外还有两个通知:“MapViewDidLoad”和“MapTimeExtentChanged”,用法同上。

2.事件响应

    其他地图事件响应的设计遵循Delegation模式,调用了MapView并想获得地图事件响应的类必须先引用对应的委托,这样特定事件触发后会通知这个类进行处理。



图3-2-1 Delegation设计模式
    下面先简单介绍下MapView中主要的三个委托,分别是:

图层委托(Layer Delegate)

    图层委托中封装了地图与图层加载相关的事件响应,包括:



图3-2-2 图层委托
触摸
4000
委托(Touch Delegate)

    触摸委托中封装了与触摸动作相关的事件响应,包括:



图3-2-3 触摸委托
弹出框委托(CalloutDelegate)

    弹出框委托中封装了与弹出框相关的事件响应,包括:



图3-2-4 弹出框委托
    委托的用法也非常简单,以图层委托为例,先在头文件中引用<AGSMapViewLayerDelegate>,然后在类文件中实现该委托中对应的方法:

//1. 引用委托

@interface ViewController: UIViewController <AGSMapViewLayerDelegate>

...

@end

 

@implementation ViewController

//2. 实现委托对应的方法

- (void)mapView:(AGSMapView*) mapView failedLoadingLayerForLayerView:(UIView *) layerView baseLayer:(BOOL)baseLayer withError:(NSError *) error {

    //出错了做处理

    //A. 删除出错图层

    if(!baseLayer)

        [self.mapViewremoveMapLayerWithName:layerView.name];

    //B. 然后重新加载图层

   AGSTiledMapServiceLayer* tiledLyr = (AGSTiledMapServiceLayer*)laverView.agsLayer;

   [tiledLyr resubmitWithURL:url credential:cred];

}

//3. 设定图层委托

- (void)viewDidLoad {

    ...

    self.mapView.layerDelegate= self;

}

    除了这里给出的响应方法,开发者也可以自定义委托来对地图事件或动作做出响应。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐