五、ArcGIS Runtime SDK for iOS 100.2.1教程系列之定位获取与显示

        一般来讲,ArcGIS的地图服务被实际运用时,使用的座标系并不会是常见的座标系,政府机关单位可能更多使用北京座标系,但是也会在一定程度上进行加密,所以不建议使用苹果自带的定位API进行定位操作,免除座标转换的麻烦,当然,如若想测试或者研究,请自行探索,本例利用的是ArcGIS自带的定位功能:

@property (nonatomic, strong) NSTimer *locationCatchTimer;//定位采集定时器

[self.mapView.locationDisplay setDataSourceStatusChangedHandler:^(BOOL started) {
        if (started) {
            [weakSelf.mapView.locationDisplay startWithCompletion:nil];
        }
    }];
    [self.mapView.locationDisplay.dataSource startWithCompletion:nil];
    self.locationCatchTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(catchLocation) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.locationCatchTimer forMode:NSDefaultRunLoopMode];

以上,我采用了定时器进行定位信息的采集,而不是在block回调中获取,是因为block回调的定位信息不及时,甚至是空的。

定时器调用的方法:

- (void)catchLocation {
    AGSPoint *currentLocationPoint = [self.mapView.locationDisplay mapLocation];
    //该例适用于启动地图后第一次定位,多次定位的判断可自行处理逻辑
    if (currentLocationPoint.x != 0) {
        [self.locationCatchTimer invalidate];
        //此处的currentLocationPoint.y-1000000是一个例子,针对企业会对座标系进行额外加密的情况
        //未加密,则无需减少或增加
        //具体怎么操作,可询问服务发布者
        AGSPoint* myMarkerPoint = [AGSPoint pointWithX:currentLocationPoint.x y:(currentLocationPoint.y-1000000) spatialReference:self.mapView.spatialReference];
        //不使用自带的定位显示,因为加密了的定位信息,所以自带显示的也不准
        self.mapView.locationDisplay.showLocation = NO;
        //拉大地图聚焦到定位位置
        [self.mapView setViewpointCenter:myMarkerPoint scale:2000 completion:^(BOOL finished) {
            
        }];
        //自己创建一个定位图标展示到地图上
        AGSPictureMarkerSymbol *pictureSymbol = [[AGSPictureMarkerSymbol alloc] initWithImage:[UIImage imageNamed:@"marker-blue"]];
        AGSGraphic *locationGraphic = [[AGSGraphic alloc] initWithGeometry:myMarkerPoint symbol:pictureSymbol attributes:nil];
        //overlayer是渲染图层,可参见教程第一篇文章
        [self.overlayer.graphics addObject:locationGraphic];
        //定位服务停止
        self.locationCatchTimer = nil;
        [self.mapView.locationDisplay.dataSource stop];
        [self.mapView.locationDisplay stop];
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章