原文地址:
http://www.giser.net/?p=20
http://tm.esrichina-bj.cn/tm/tech/?p=604
在前面的章节中,我们已经知道,如何在Apple OS设备上显示地图,不论动态地图服务还是静态地图服务,同时认识了ArcGIS API for iOS中的地图组件——MapView。同其他ArcGIS APIs类似,ArcGIS API for iOS同样也具有自定义数据的Graphics Layer。下面便来介绍在【ArcGIS API for iOS开发之旅】Hello,Map的地图上自定义数据添加的实现。 1、首先按照【ArcGIS API for iOS开发之旅】Hello,Map的新建地图应用程序的步骤创建一个名为GraphicsDemo的程序,同时将地图显示的前期工作准备好。
2、打开GraphicsDemoViewController.h文件,在GraphicsDemoViewController类中定义 AGSGraphicsLayer对象,并把这个定义声明为该类的属性。此外,在声明中添加对AGSMapViewDelegate的支持。代码如下(绿 色代码表示)
@interface GraphicsDemoViewController : UIViewController
{
AGSMapView *_mapView;
//定义AGSGraphicsLayer对象
AGSGraphicsLayer *graphicsLayer;
//定义isAddPoint,是否增加点
BOOL isAddPoint;
}
@property (nonatomic, retain) IBOutlet AGSMapView *mapView;
//使用IBOutlet定义graphicsLayer
@property (nonatomic, retain) IBOutlet AGSGraphicsLayer *graphicsLayer;
- (IBAction)swithAddPoint:(id)sender;
@end
3、打开GraphicsDemoViewController.m文件,进行graphicsLayer属性的声明。代码如下(绿色表示)
@implementation GraphicsDemoViewController
@synthesize mapView = _mapView;
@synthesize graphicsLayer;
4、在GraphicsDemoViewController.m文件中找到viewDidLoad函数,将graphicsLayer添加到mapView中,并且设置mapView的mapViewDelegate为self。如下代码所示
– (void)viewDidLoad {
[superviewDidLoad];
self.mapView.mapViewDelegate = self;
AGSTiledMapServiceLayer *tiledLayer = [[AGSTiledMapServiceLayer alloc]
initWithURL:[NSURL URLWithString:kTiledMapServiceURL ]];
[self.mapView addMapLayer:tiledLayerwithName:"Tiled Layer"];
[tiledLayer release];
self.graphicsLayer = [AGSGraphicsLayer graphicsLayer];
[self.mapView addMapLayer:self.graphicsLayer withName:"graphicsLayer"];
isAddPoint = YES;
}
5、在GraphicsDemoViewController.m文件中添加如下代码,实现点击地图时触发的事件,也即在地图上添加自定义数据时触发的事件
- (void)mapView:(AGSMapView *)mapViewdidClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint graphics:(NSDictionary *)graphics{
if (isAddPoint)
{
//定义一个图片符号 本例选用图钉图片
AGSPictureMarkerSymbol *pt = [AGSPictureMarkerSymbol pictureMarkerSymbol WithImageNamed:"pushpin.png"];
// 定义图片在地图上的偏移量。默认是图片的左下角,需要进行偏移调整,将图片的中心放在地图上
pt.xoffset = 8;
pt.yoffset = -18;
//定义一个弹出气泡的模板
pointInfoTemplate *pointTemplate = [[pointInfoTemplate alloc] init];
AGSGraphic *pushpin = [[AGSGraphic alloc] initWithGeometry:mappoint symbol:pt attributes:nil infoTemplate:pointTemplate];
// 将图钉添加到Graphics Layer中
[self.graphicsLayer addGraphic:pushpin];
[pushpin release];
[pointTemplate release];
//刷新
[self.graphicsLayer dataChanged];
}
}
6、进行编译,运行。效果如图所示