前言
AGSMapView是用于展示2D地理信息的一個類,暴露了mapScale、rotation等常用屬性,也提供了設置視角,設置比例尺,監聽用戶行為等事件的接口。
加載底圖
加載底圖需要賦值一個AGSMap對象給AGSMapView
let amapUrl = "http://webst03.is.autonavi.com/appmaptile?x={col}&y={row}&z={level}&lang=zh_cn&size=1&scale=1&style=7"
let tiledLayer = AGSWebTiledLayer(urlTemplate: amapUrl);
let map = AGSMap(basemap: AGSBasemap(baseLayer: tiledLayer))
mapView.map = map
添加數據層
上文講到,數據層也叫操作層,是用戶主要接觸的圖層,改圖層采用自上而下的順序進行繪制
let featureTable = AGSServiceFeatureTable(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0")!)
let featureLayer = AGSFeatureLayer(featureTable: featureTable)
map.operationalLayers.add(featureLayer)
添加圖形層
圖形、繪制層內容較多,會單獨拿出來說
監聽用戶點擊事件并且獲取相關屬性信息
- 設置代理
mapView.touchDelegate = self
- 實現代理方法
extension ViewController: AGSGeoViewTouchDelegate{
func geoView(_ geoView: AGSGeoView, didTouchUpAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
}
}
- 代理方法中Tap、Touch、Double Tap、Long Press、Force Touch都有對應方法
定位+顯示當前位置
- 定位ArcGIS本身有封裝好的api提供使用
自動獲取定位并且把地圖視角設置到當前位置:
self.mapView.locationDisplay.start { error in
if let e = error{
}
}
self.mapView.locationDisplay.autoPanMode = AGSLocationDisplayAutoPanMode.compassNavigation
self.mapView.locationDisplay.navigationPointHeightFactor = 0.5
ps:iOS需要在plist文件中添加相應的權限字段
- 也可以通過系統api或者第三方提供的定位api來獲取經緯度,然后手動設置視角。這里要記得做不同坐標系的轉換工作
通過不同方式設置視角
mapView.setViewpoint(AGSViewpoint(latitude: 34.752965,longitude: 113.665911,scale: scale)) { result in
}
獲取當前的比例尺
有相應的get方法直接獲取
mapView.mapScale
設置地圖的比例尺
mapView.setViewpointScale(mapView.mapScale * 2, completion: nil)
監聽地圖視角變化
/** Block that gets invoked whenever the viewpoint changes.
@note This handler may get invoked up to 60 times per second, for example, when a pan or zoom animation is in progress. Do not perform any heavy-lifting in this handler as it may adversely impact the rendering performance.
@note The block will be invoked on the same thread on which the event occured, which could be any arbitrary thread. You need to dispatch any UI related work to the main thread.
@since 100
*/
@property (nullable, nonatomic, copy, readwrite) void (^viewpointChangedHandler)(void);