近段时间因为项目需求,所以需要再arcgis上边通过在地图上进行点击,之后把各个点击点按顺序连接起来,同时将两点之间的距离给计算出来并显示。故此编写本博客
具体分为以下几个步骤
1、添加地图并显示到视图中(次步省略掉)
2、在地图上添加AGSGraphicsLayer和AGSSketchGraphicsLayer这两个图层
3、指定指定相应的delegate(主要是mapview的layerDelegate和touchDelegate)
4、在需要开始绘制点击路径以及测距的时候做一些初始化设置(主要是设置SketchLayer的一些属性)
5、在代理方法中实现测距、图层中的测距结果的显示问题(主要是自定义一些合适的markSymbol)
1、添加地图并显示到视图中(本博客中次步省略掉)
2、在地图上添加AGSGraphicsLayer和AGSSketchGraphicsLayer这两个图层
2.1、定义实例变量
@property(strong, nonatomic) AGSGraphicsLayer *graphicLayer;
@property(strong, nonatomic) AGSSketchGraphicsLayer *sketchLayer;
3、指定指定相应的delegate(主要是mapview的layerDelegate和touchDelegate)
_mapView.layerDelegate = self;
_mapView.touchDelegate = self;
4、在需要开始绘制点击路径以及测距的时候做一些初始化设置(主要是设置SketchLayer的一些属性)
//we remove the previos part from the sketch layer as we are going to start a new path.
[self.sketchLayer removePartAtIndex:0];
//add a new path to the geometry in preparation of adding vertices to the path
[self.sketchLayer addPart];
除了这些初始化的设置之外,可能还需要对graphicLayer以及sketchLayer进行移除掉之前添加到地图上的一些图层,视具体情况而定
5、在代理方法中实现测距、图层中的测距结果的显示问题(主要是自定义一些合适的markSymbol)
调用如下的代理方法,当然在调用该方法的时候要注意让本类实现代理“AGSMapViewLayerDelegate”以及"AGSMapViewTouchDelegate"
- (void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
5.1、关于测距:定义两个"NSMutableArray",一个保存各个点击点(vertices)以及相邻两个点击点的中心位置(midvertices),前者是为了计算距离而保留下来的,后者则是为了将计算出来的距离给显示出来的位置而做的记录
5.1.1、测距:通过调用方法AGSPoint中的方法
-(double)distanceToPoint:(AGSPoint*)other
5.1.2、记录中心点以及顶点,顶点就不说了直接讲mappoint添加到相应的可变数组中就可以了,中心点则需要自己计算出来,相应的计算方法很简单就是用简单的数学方法,讲两个点的x坐标以及y坐标分别相加在除以2,即是:mid.x = (a.x + b.x)/2 ;mid.y = (a.y + b.y)/2 之后将之组装成一个AGSPoint类型的数据并保存到相应的顶点坐标里边就可以了
5.1.3、绘制自定义的symbol,在这里我有一个简单的例子(直接贴代码了),你可以创建自己的
AGSCompositeSymbol *cs = [AGSCompositeSymbol compositeSymbol];
// create outline
AGSSimpleLineSymbol *sls = [AGSSimpleLineSymbol simpleLineSymbol];
sls.color = [UIColor greenColor];
sls.width = 2;
sls.style=AGSSimpleLineSymbolStyleSolid;
// create main circle
AGSSimpleMarkerSymbol *sms = [AGSSimpleMarkerSymbol simpleMarkerSymbol];
sms.color = [UIColor whiteColor];
sms.outline = sls;
sms.size = CGSizeMake(30, 30);
sms.style=AGSSimpleMarkerSymbolStyleCircle;
//create text to display the distance
AGSTextSymbol *ts = [[AGSTextSymbol alloc] initWithText:[NSString stringWithFormat:@"%@",distance] color:[UIColor blueColor]];
ts.backgroundColor = [UIColor whiteColor];
ts.vAlignment = AGSTextSymbolVAlignmentMiddle;
ts.hAlignment = AGSTextSymbolHAlignmentCenter;
ts.fontSize = 14;
//add the symbol to compositeSymbol
[cs addSymbol:sms];
[cs addSymbol:ts];
5.1.4、将相应的测距数据给显示出来,同时将中点的默认symbol给去掉
AGSGraphic *theGraphic = [[AGSGraphic alloc] initWithGeometry:midPoint symbol:cs attributes:nil];
[self.graphicLayer addGraphic:theGraphic];
self.gpsSketchLayer.midVertexSymbol = nil;
5.2、将当前的点击点给添加到sketch layer的vertice中
[self.sketchLayer insertVertex:thePoint inPart:0 atIndex:-1];
上边的是主要的实现细节,若有遗漏以及不够合理的地方可多多交流。
下图是实现的效果图: