MapView Delegates

Download Sample Application

You can interact with the map using natural gestures offered by your Mac's trackpad or mouse. In this sample the geographical location (x,y) of the cursor is displayed and updated as you move the cursor across the map. The snippet below shows you how the MapView touch delegate protocol is defined, and how the didMoveMouseToPoint touch delegate method is implemented to calculate the x,y coordinates each time the cursor is moved.

//header file
@interface MapViewDelegatesSample : NSViewController <AGSMapViewTouchDelegate>
@property (strong) IBOutlet AGSMapView *mapView;
@end

//implementation file
//set mapView's touch delegate so when user interact with map
//respective delegate method will be called
self.mapView.touchDelegate = self;

//  this will be called when you move the mouse on the map
- (void)mapView:(AGSMapView *)mapView didMoveMouseToPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint {
    ...
}

Sample Code

//SWIFT SAMPLE CODE
/*
Copyright 2014 Esri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import Cocoa
import ArcGIS

//layer urls
let kBaseMapURL = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"

public class MapViewDelegatesSwiftSample: NSViewController, AGSMapViewLayerDelegate, AGSMapViewTouchDelegate {

    @IBOutlet weak var mapView:AGSMapView!
    @IBOutlet weak var currentLocationLabel:NSTextField!
    
    //MARK: - awakeFromNib
    
    public override func awakeFromNib() {
        
        //set mapView's layer delegate so when mapViewDidLoad called
        self.mapView.layerDelegate = self
        
        //set mapView's touch delegate so when user interact with map
        //respective delegate method will be called
        self.mapView.touchDelegate = self
        
        //show magnifier on tap and hold
        self.mapView.showMagnifierOnTapAndHold = true
        
        //track mouse movement
        self.mapView.trackMouseMovement = true
        
        //add base layer
        let baseMapLayer = AGSTiledMapServiceLayer(URL: NSURL(string: kBaseMapURL))
        self.mapView.addMapLayer(baseMapLayer, withName: "Base Layer")
        
        //zoom to predefined extend with known spatial reference of the map
        let envelope = AGSEnvelope(xmin: -1131596.019761, ymin:3893114.069099, xmax:3926705.982140, ymax:7977912.461790, spatialReference:AGSSpatialReference.webMercatorSpatialReference())
        
        self.mapView.zoomToEnvelope(envelope, animated: true)
    }
    
    //MARK: - MapView Layer Delegate
    
    // -------------------------------------------------------------------------------
    //  mapViewDidLoad:mapView
    //
    //  this will be called when mapView loads successfully
    // -------------------------------------------------------------------------------
    public func mapViewDidLoad(mapView: AGSMapView!) {
        println("MapView loaded successfully!")
    }
    
    //MARK: - MapView Touch Delegate
    
    // -------------------------------------------------------------------------------
    //  mapView:shouldProcessClickAtPoint:mapPoint
    //
    //  this will be called everytime user tap on the map
    // -------------------------------------------------------------------------------
    public func mapView(mapView: AGSMapView!, shouldProcessClickAtPoint screen: CGPoint, mapPoint mappoint: AGSPoint!) -> Bool {
        println("MapView's shouldProcessClickAtPoint invoked!")
        return true
    }
    
    // -------------------------------------------------------------------------------
    //  mapView:didClickAtPoint:mapPoint:graphics
    //
    //  this will be called if mapView touch delegate's shouldProcessClickAtPoint returns YES
    // -------------------------------------------------------------------------------
    public func mapView(mapView: AGSMapView!, didClickAtPoint screen: CGPoint, mapPoint mappoint: AGSPoint!, features: [NSObject : AnyObject]!) {
        println("MapView's didClickAtPoint invoked!")
    }
    
    // -------------------------------------------------------------------------------
    //  mapView:didTapAndHoldAtPoint
    //
    //  this will be called when user tap and hold on map
    // -------------------------------------------------------------------------------
    public func mapView(mapView: AGSMapView!, didTapAndHoldAtPoint screen: CGPoint, mapPoint mappoint: AGSPoint!, features: [NSObject : AnyObject]!) {
        println("MapView's didTapAndHoldAtPoint invoked!")
    }
    
    // -------------------------------------------------------------------------------
    //  mapView:didMoveTapAndHoldAtPoint
    //
    //  this will be called when user tap and hold on map and move cursor
    // -------------------------------------------------------------------------------
    public func mapView(mapView: AGSMapView!, didMoveTapAndHoldAtPoint screen: CGPoint, mapPoint mappoint: AGSPoint!, features: [NSObject : AnyObject]!) {
        println("MapView's didMoveTapAndHoldAtPoint invoked!")
    }
    
    // -------------------------------------------------------------------------------
    //  mapView:didEndTapAndHoldAtPoint
    //
    //  this will be called when user release cursor from map after tap and hold
    // -------------------------------------------------------------------------------
    public func mapView(mapView: AGSMapView!, didEndTapAndHoldAtPoint screen: CGPoint, mapPoint mappoint: AGSPoint!, features: [NSObject : AnyObject]!) {
        println("MapView's didEndTapAndHoldAtPoint invoked!")
    }
    
    // -------------------------------------------------------------------------------
    //  mapView:didMoveMouseToPoint:screen:mapPoint
    //
    //  this will be called when you move mouser on map
    // -------------------------------------------------------------------------------
    public func mapView(mapView: AGSMapView!, didMoveMouseToPoint screen: CGPoint, mapPoint mappoint: AGSPoint!) {
        self.currentLocationLabel.stringValue = String(format: "X:%.8f, Y:%.8f", mappoint.x, mappoint.y)
    }
    
}
//OBJECTIVE C SAMPLE CODE
/*
 Copyright 2013 Esri
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#import "MapViewDelegatesSample.h"

//layer urls
#define kBaseMapURL @"http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"

@interface MapViewDelegatesSample ()

@end

@implementation MapViewDelegatesSample

#pragma mark - awakeFromNib

// -------------------------------------------------------------------------------
//  awakeFromNib
// -------------------------------------------------------------------------------
- (void)awakeFromNib {
    
    //set mapView's layer delegate so when mapViewDidLoad called
    self.mapView.layerDelegate = self;
    
    //set mapView's touch delegate so when user interact with map
    //respective delegate method will be called
    self.mapView.touchDelegate = self;
    
    //show magnifier on tap and hold
    self.mapView.showMagnifierOnTapAndHold = YES;
    
    //track mouse movement
    self.mapView.trackMouseMovement = YES;
    
    //add base layer
	AGSTiledMapServiceLayer *baseMapLayer = [[AGSTiledMapServiceLayer alloc] initWithURL:[NSURL URLWithString:kBaseMapURL]];
    [self.mapView addMapLayer:baseMapLayer withName:@"Base Layer"];
    
    //zoom to predefined extend with known spatial reference of the map
    AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:-1131596.019761 ymin:3893114.069099 xmax:3926705.982140 ymax:7977912.461790 spatialReference:[AGSSpatialReference webMercatorSpatialReference]];
    [self.mapView zoomToEnvelope:envelope animated:YES];
}

#pragma mark - MapView Layer Delegate

// -------------------------------------------------------------------------------
//  mapViewDidLoad:mapView
//
//  this will be called when mapView loads successfully
// -------------------------------------------------------------------------------
- (void)mapViewDidLoad:(AGSMapView *)mapView {
    NSLog(@"MapView loaded successfully!");
}

#pragma mark - MapView Touch Delegate

// -------------------------------------------------------------------------------
//  mapView:shouldProcessClickAtPoint:mapPoint
//
//  this will be called everytime user tap on the map
// -------------------------------------------------------------------------------
- (BOOL)mapView:(AGSMapView *)mapView shouldProcessClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint {
    NSLog(@"MapView's shouldProcessClickAtPoint invoked!");
    return YES;
}

// -------------------------------------------------------------------------------
//  mapView:didClickAtPoint:mapPoint:graphics
//
//  this will be called if mapView touch delegate's shouldProcessClickAtPoint returns YES
// -------------------------------------------------------------------------------
- (void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features {
        NSLog(@"MapView's didClickAtPoint invoked!");
}

// -------------------------------------------------------------------------------
//  mapView:didTapAndHoldAtPoint
//
//  this will be called when user tap and hold on map
// -------------------------------------------------------------------------------
- (void)mapView:(AGSMapView *)mapView didTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features {
    NSLog(@"MapView's didTapAndHoldAtPoint invoked!");
}

// -------------------------------------------------------------------------------
//  mapView:didMoveTapAndHoldAtPoint
//
//  this will be called when user tap and hold on map and move cursor
// -------------------------------------------------------------------------------
- (void)mapView:(AGSMapView *)mapView didMoveTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features {
    NSLog(@"MapView's didMoveTapAndHoldAtPoint invoked!");
}

// -------------------------------------------------------------------------------
//  mapView:didMoveTapAndHoldAtPoint
//
//  this will be called when user release cursor from map after tap and hold
// -------------------------------------------------------------------------------
- (void)mapView:(AGSMapView *)mapView didEndTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features {
    NSLog(@"MapView's didEndTapAndHoldAtPoint invoked!");
}

// -------------------------------------------------------------------------------
//  mapView:didMouseDownAtPoint:mapPoint:mapPoint
//
//  this will be called when you move mouser on map
// -------------------------------------------------------------------------------
- (void)mapView:(AGSMapView *)mapView didMoveMouseToPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint {
    //NSLog(@"MapView's didMoveMouseToPoint invoked!");
    self.currentLocationLabel.stringValue = [NSString stringWithFormat:@"X:%.8f, Y:%.8f",mappoint.x,mappoint.y];
}

@end
//OBJECTIVE C SAMPLE CODE
/*
 Copyright 2013 Esri
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#import <Cocoa/Cocoa.h>

@interface MapViewDelegatesSample : NSViewController <AGSMapViewLayerDelegate,AGSMapViewTouchDelegate>

@property (strong) IBOutlet AGSMapView *mapView;
@property (strong) IBOutlet NSTextField *currentLocationLabel;

@end
Feedback on this topic?