ArcGIS Runtime SDK for iOS(二) ---使用定时器绘制由point组成的polyLine,并显示位置坐标

来源:互联网 发布:线切割3b编程软件 编辑:程序博客网 时间:2024/05/17 06:57

by. SevenJohs
2016.4.14 武汉 晴

我科传奇二十年,今日谢幕。
这里写图片描述

进正题~

  • 内容
    使用定时器绘制由point组成的polyLine。

  • 概述

    • 由自定义函数生成100个待连线的点;
    • 设置重复调用定时器;
    • 绘制polyLine(使用update);
    • layout信息展示。
  • 预备知识
    定时器:

    调用一次计时器方法:

//调用一次计时器myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:NO]; 

重复调用计时器方法:

//每秒运行一次function方法。 timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(function:) userInfo:nil repeats:YES];  
    在折线上取所点击的点,遇到困难,也是各种咨询,老大云淡风轻地告诉我有这个东西:AGSGeometryEngine 。        官方解释是这样的:        Instances of this class represents engines that can perform geometric operations locally on the device.        AGSGeometryEngine is functionally similar to AGSGeometryServiceTask except that it does not rely on a remote ArcGIS Server Geometry Service.        You can use the engine even when the device does not have any network connectivity.        我的理解是,它的实例对象提供了交互操作几何的功能,特点是可以在没有网络支持下进行交互。        主要方法:
//将给定的geometry,以新的spatialReference展现。- (AGSGeometry*)projectGeometry:(AGSGeometry*)geometry toSpatialReference:(AGSSpatialReference*)spatialReference;//通过使用二维笛卡尔数学计算对应Geometry pass in的平面面积。-(double)areaOfGeometry:(AGSGeometry*)geometry;//计算对应在地球椭球面上的几何面积。-(double)shapePreservingAreaOfGeometry:(AGSGeometry*)geometry inUnit:(AGSAreaUnits)areaUnit;//获取指定geometry长度-(double)lengthOfGeometry:(AGSGeometry*)geometry;//超级重要的两个方法/** Finds the nearest vertex in a specified geometry to a specified point.(只包含顶点)-(AGSProximityResult*)nearestVertexInGeometry:(AGSGeometry*)geometry toPoint:(AGSPoint*)point;/** Finds the nearest coordinate in a specified geometry to a specified point.(包含几何上的所有点)-(AGSProximityResult*)nearestCoordinateInGeometry:(AGSGeometry*)geometry toPoint:(AGSPoint*)point;
  • 由自定义函数生成100个待连线的点
/** *  绘制点 */-(void)drawPoint{    CGPoint randPoint = CGPointMake(114.0, 30.0);    CGPoint newPoint = CGPointZero;    for(int i = 0;i < 100;i++){        randPoint.x = newPoint.x + (i * POINT_FUNCTION_MAX);        randPoint.y = newPoint.y + (i * POINT_FUNCTION_MIN);        AGSPoint* mapPoint = [[AGSPoint alloc]initWithX:randPoint.x y:randPoint.y spatialReference:_graphicLayer.spatialReference];        AGSPictureMarkerSymbol *pictureMarkerSymbol = [AGSPictureMarkerSymbol pictureMarkerSymbolWithImageNamed:@"2.png"];        _graphic = [[AGSGraphic alloc]initWithGeometry:mapPoint symbol:pictureMarkerSymbol attributes:nil infoTemplateDelegate:nil];        [_graphicLayer addGraphic:_graphic];        newPoint = randPoint;    }    [self calloutApperanceSet];}
  • 设置重复调用定时器
/** *  定义定时器 * *  @return timer */-(void)timerDefination{    //updatePloyline 更新时调用timerDefination    _timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(updatePloyline) userInfo:nil repeats:YES];    [_timer fire];//启动计时器}/** *  停止定时器 * *  @return */-(void)stopTimer{    [_timer setFireDate:[NSDate distantFuture ]];}
  • 绘制polyLine(使用remove & add)

    在一个Graphic上显示绘制折线的进度,有两种方式:1.removeGraphic,再add;2.根据GUID updateGraphic;3.直接根据Graphic对应的Geometry进行update.

    亲测证明:iOS上只能使用以上第一种方法。
    下面使用第一种方法来绘制:

/** *  绘制线条 */-(void)drawPolyLine{    _pointIndex = 0;    _polyline = nil;    _polylineSymbol = [AGSSimpleLineSymbol simpleLineSymbolWithColor:[UIColor blueColor] width:4];    [self timerDefination];}/** *  更新PolyLine,使其绘制出每隔20个点,绘制出独立的线段。 */-(void)updatePloyline {    //条件性判断    if(!_mutPointArr || [_mutPointArr count] <= _pointIndex) {        return;    }    if(!_polyline) {        _polyline = [[AGSMutablePolyline alloc]init];        //先添加路径,才能在路径的基础上添加点        [_polyline addPathToPolyline];    }    //[非连续点设置]每20个点,结束画线,重新添加路径 addPathToPolyLine    if(_pointIndex%20 == 0 ){        [_polyline addPathToPolyline];    }    //添加点到路径    [_polyline addPointToPath:_mutPointArr[_pointIndex]];    //remove    if(_pointIndex>0) {        if(!_graphic) {            [_graphicLayer removeGraphic:_graphic];        }        _graphic = [[AGSGraphic alloc]initWithGeometry:_polyline symbol:_polylineSymbol attributes:nil infoTemplateDelegate:nil];        //add        [_graphicLayer addGraphic:_graphic];    }    //每次定时器的执行,索引+1    _pointIndex++;}
  • 添加callout显示当前位置经纬度(实现AGSMapViewTouchDelegate的点击事件):
//实现AGSMapViewTouchDelegate的didClickAtPoint方法#pragma mark AGSMapViewTouchDelegate-(void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint graphics:(NSDictionary *)graphics{    AGSPoint* locationPnt = nil;    AGSGraphic* targetGraphic = nil;    //遍历字典中的所有graphics,取得点击事件对应的geometry    for (NSArray* graphicArr in graphics.allValues) {        targetGraphic = graphicArr[0];        if(targetGraphic.geometry) {            //若对应geometry为point,则将几何点的geometry强制转换,得到点击位置坐标            if([targetGraphic.geometry isKindOfClass:[AGSPoint class]] ) {                locationPnt = (AGSPoint*)targetGraphic.geometry;            } else if ([targetGraphic.geometry isKindOfClass:[AGSPolyline class]]) {            //若是polyline,则使用GeometryEngine                AGSGeometryEngine* geoEngine = [[AGSGeometryEngine alloc]init];                //使用包含几何上所有的点,寻找点击位置最近的一个坐标点                AGSProximityResult*  pr = [geoEngine nearestCoordinateInGeometry:targetGraphic.geometry toPoint:mappoint];                locationPnt = pr.point;            }        }    }    if(!targetGraphic || !locationPnt) {        return;    }    _mapView.callout.title = [NSString stringWithFormat:@"x:%f y:%f",locationPnt.x,locationPnt.y];    [_mapView.callout showCalloutAtPoint:locationPnt forGraphic:targetGraphic animated:YES];}END 
1 0
原创粉丝点击