首先需要遵守AGSMapView的AGSGeoViewTouchDelegate協議,實現其代理方法,在點擊地圖某一個點時會獲得回調,在回調的位置需要進行檢索查詢,在PC端就是所謂的i查詢:
- (void)geoView:(AGSGeoView *)geoView didTapAtScreenPoint:(CGPoint)screenPoint mapPoint:(AGSPoint *)mapPoint {
//進行i查詢,tolerance大致是指精確的範圍,api有英文解釋
__weak __typeof(self)weakSelf = self;
[geoView identifyLayersAtScreenPoint:screenPoint tolerance:2.0 returnPopupsOnly:NO completion:^(NSArray<AGSIdentifyLayerResult *> * _Nullable identifyResults, NSError * _Nullable error) {
if (identifyResults.count) {
//得到的i查詢結果一般是最大層的圖層信息,這裏得到的是動態圖層信息
AGSIdentifyLayerResult *result = identifyResults.firstObject;
if (result.sublayerResults.count) {
//當然,咱們要的是圖層上的元素信息,此處取第一個來高亮
AGSIdentifyLayerResult *oneLayer = result.sublayerResults.firstObject;
id<AGSGeoElement> geo = oneLayer.geoElements.firstObject;
if (geo.geometry.geometryType == AGSGeometryTypePolyline || geo.geometry.geometryType == AGSGeometryTypePolygon) {
//這裏用空心實線來高亮用戶點擊的元素,具體用什麼符號渲染,自行根據點擊的元素類型枚舉選擇
AGSSimpleFillSymbol *fillSymble = [AGSSimpleFillSymbol simpleFillSymbolWithStyle:AGSSimpleFillSymbolStyleSolid color:[UIColor clearColor] outline:[AGSSimpleLineSymbol simpleLineSymbolWithStyle:AGSSimpleLineSymbolStyleSolid color:[UIColor redColor] width:2.0]];
//創建並將符號渲染到overlayer
AGSGraphic *myGraphic = [AGSGraphic graphicWithGeometry:geo.geometry symbol:fillSymble attributes:geo.attributes];
[weakSelf.overlayer.graphics removeAllObjects];
[weakSelf.overlayer.graphics addObject:myGraphic];
//移動到用戶點擊的點,自行選擇是否需要,自帶動畫
[weakSelf.mapView setViewpointCenter:mapPoint completion:^(BOOL finished) {
//結束回調
}];
}
}
}else{
//父圖層沒有子圖層時
id<AGSGeoElement> geo = result.geoElements.firstObject;
[weakSelf.mapView setViewpointGeometry:geo.geometry padding:1.0 completion:^(BOOL finished) {
}];
}
}];
}
元素的類型或者說形狀類型爲一個枚舉:
typedef NS_ENUM(NSInteger, AGSGeometryType) {
AGSGeometryTypeUnknown = -1, /*!< Undefined未定義 */
AGSGeometryTypePoint = 1, /*!< Point點 */
AGSGeometryTypeEnvelope = 2, /*!< Envelope包絡線 */
AGSGeometryTypePolyline = 3, /*!< Polyline 線*/
AGSGeometryTypePolygon = 4, /*!< Polygon 多邊形*/
AGSGeometryTypeMultipoint = 5, /*!< Multipoint 多個點*/
};
i查詢的速度與服務器的性能,網絡的情況也有莫大關係,所以有時候還是有點慢的。