一、前言
最近項目中的一個問題困擾了好幾天,前前后后對比代碼,調試這個地圖縮放級別的問題花費了不少的精力。
在這里就分享下整個過程。
二、設置地圖縮放級別
這個對於任何地圖API來說都是最基本的功能,在 ArcGIS API 中是這樣:
this.map = new this.$Gis_api.Map({ basemap: new this.$Gis_api.Basemap({ baseLayers: [layer_obj.tdtvecLayer] })
}) // 初始化mapview this.mapview = new this.$Gis_api.MapView({ container: this.map_code, center: [120.246402, 30.309779], spatialReference: new this.$Gis_api.SpatialReference({ wkid: 3857 }), map: this.map, scale: 108335, constraints: { snapToZoom: false, minScale: 280000 } })
設置縮放級別是:constraints 下的:lods、minScale、maxScale、minZoom、maxZoom。
其中 maxScale 默認是0,一般看情況設置。
三、設置無效的問題
按照上面寫好后,但是地圖只能縮放到 17 級,scale 到5000 左右,不能再縮放下去。
找各種代碼、問題,看代碼寫法和上面一樣。所以問題是在其他地方。
在一步一步調試的過程中發現了問題:
從圖中看到,mapview 中的 constraints 以 effective 開頭的幾個屬性限制了。
查看了 API 文檔,這幾個屬性是只讀的,那是從哪里獲取到的這些屬性呢?
再次調試(大學老師教的,遇到問題就要調調試試,問題就自現了)。
從 new MapView 一路調試到 mapview.when ,這時 constraint 下的幾個只讀屬性值發生變化。
那應該就是加載圖層導致縮放出現問題。
最終問題:
1、初始加載底圖時,底圖是發布的服務,服務屬性設置了緩存只到17級
2、雖然設置了 mapview 的縮放范圍,但是會讀取第一個加載圖層縮放屬性,且不可改變
找到問題后做了以下修改:
// 初始化地圖實例 this.map = new this.$Gis_api.Map({ basemap: new this.$Gis_api.Basemap({ baseLayers: [] }) }) // 初始化mapview this.mapview = new this.$Gis_api.MapView({ container: this.map_code, center: [120.246402, 30.309779], spatialReference: new this.$Gis_api.SpatialReference({ wkid: 3857 }), map: this.map, scale: 108335, constraints: { snapToZoom: false, minScale: 280000 } }) // 加載空圖層,縮放范圍就不會有限制 this.baseDataLayer = new this.$Gis_api.GraphicsLayer({ id: 'baseDataLayer', maxScale: 4000, minScale: 300000 }) this.map.add(this.baseDataLayer)
修改后,地圖就可以縮放到19級了。