一般來講,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];
}
}