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

    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 defaultCenteraddObserver:selfselector:@selector(respondToEnvChange:)name:@"MapDidEndPanning"object:nil];

    // register for "MapDidEndZooming"notifications

    [[NSNotificationCenter defaultCenteraddObserver: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 图层委托

触摸委托(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;

}

    除了这里给出的响应方法,开发者也可以自定义委托来对地图事件或动作做出响应。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值