@@ -42,55 +42,55 @@ import { WebMap as WebMapV3 } from './webmap/v3/WebMap';
4242 * @usage
4343 */
4444export class WebMap extends mapboxgl . Evented {
45- constructor ( id , options = { } , mapOptions ) {
46- super ( ) ;
47- this . mapId = id ;
45+ constructor ( id , options = { } , mapOptions ) {
46+ super ( ) ;
47+ this . mapId = id ;
4848 this . options = Object . assign ( { } , options ) ;
4949 this . options . server = this . _formatServerUrl ( options . server ) ;
5050 this . options . target = options . target || 'map' ;
5151 this . options . withCredentials = options . withCredentials || false ;
5252 this . mapOptions = mapOptions ;
53- this . _createWebMap ( ) ;
53+ this . _createWebMap ( ) ;
5454 this . on ( 'mapinitialized' , ( ) => {
5555 this . map . on ( 'remove' , ( ) => {
5656 this . _stopCanvg ( ) ;
5757 } ) ;
5858 } ) ;
59- }
60- /**
61- * @function WebMap.prototype.resize
62- * @description 地图 resize。
63- */
64- resize ( ) {
65- this . map . resize ( ) ;
59+ }
60+ /**
61+ * @function WebMap.prototype.resize
62+ * @description 地图 resize。
63+ */
64+ resize ( ) {
65+ this . map . resize ( ) ;
6666 }
6767
6868 /**
69- * @function WebMap.prototype.setMapId
70- * @param {string } mapId - webMap 地图 ID。
71- * @description 设置 WebMap ID。
72- */
69+ * @function WebMap.prototype.setMapId
70+ * @param {string } mapId - webMap 地图 ID。
71+ * @description 设置 WebMap ID。
72+ */
7373 setMapId ( mapId ) {
7474 this . mapId = mapId ;
7575 this . _createWebMap ( ) ;
7676 }
7777
7878 /**
7979 * @function WebMap.prototype.setWebMapOptions
80- * @param {Object } webMapOptions - webMap 参数。
81- * @description 设置 webMap 参数。
82- */
80+ * @param {Object } webMapOptions - webMap 参数。
81+ * @description 设置 webMap 参数。
82+ */
8383 setWebMapOptions ( webMapOptions ) {
8484 const server = this . _formatServerUrl ( webMapOptions . server ) ;
8585 this . options . server = server ;
8686 this . _createWebMap ( ) ;
8787 }
8888
8989 /**
90- * @function WebMap.prototype.setMapOptions
91- * @param {Object } mapOptions - map 参数。
92- * @description 设置 map 参数。
93- */
90+ * @function WebMap.prototype.setMapOptions
91+ * @param {Object } mapOptions - map 参数。
92+ * @description 设置 map 参数。
93+ */
9494 setMapOptions ( mapOptions ) {
9595 let { center, zoom, maxBounds, minZoom, maxZoom, isWorldCopy, bearing, pitch } = mapOptions ;
9696 center && center . length && this . map . setCenter ( center ) ;
@@ -109,22 +109,24 @@ export class WebMap extends mapboxgl.Evented {
109109 * @description 登陆窗口后添加地图图层。
110110 */
111111 _createWebMap ( ) {
112- const mapUrl = Util . transformUrl ( Object . assign ( { url : `${ this . options . server } web/maps/${ this . mapId } /map` } , this . options ) ) ;
113- this . _getMapInfo ( mapUrl ) ;
114- }
112+ const mapUrl = Util . transformUrl (
113+ Object . assign ( { url : `${ this . options . server } web/maps/${ this . mapId } /map` } , this . options )
114+ ) ;
115+ this . _getMapInfo ( mapUrl ) ;
116+ }
115117
116- /**
117- * @private
118- * @function WebMap.prototype._formatServerUrl
119- * @description 格式化服务地址
120- */
121- _formatServerUrl ( server ) {
122- let urlArr = server . split ( '' ) ;
123- if ( urlArr [ urlArr . length - 1 ] !== '/' ) {
124- server += '/' ;
125- }
126- return server ;
127- }
118+ /**
119+ * @private
120+ * @function WebMap.prototype._formatServerUrl
121+ * @description 格式化服务地址
122+ */
123+ _formatServerUrl ( server ) {
124+ let urlArr = server . split ( '' ) ;
125+ if ( urlArr [ urlArr . length - 1 ] !== '/' ) {
126+ server += '/' ;
127+ }
128+ return server ;
129+ }
128130
129131 /**
130132 * @private
@@ -133,49 +135,43 @@ export class WebMap extends mapboxgl.Evented {
133135 * @param {string } mapUrl - 请求地图的 url。
134136 */
135137 _getMapInfo ( mapUrl ) {
136- FetchRequest . get ( mapUrl , null , { withCredentials : this . options . withCredentials } )
137- . then ( ( response ) => {
138- return response . json ( ) ;
139- } )
140- . then ( ( mapInfo ) => {
141- const projectionMap = {
142- 'EPSG:4490' : 'EPSG:4490' ,
143- 'EPSG:4214' : 'EPSG:4214' ,
144- 'EPSG:4610' : 'EPSG:4610' ,
145- 'EPSG:3857' : 'EPSG:3857' ,
146- 'EPSG:4326' : 'EPSG:4326'
147- } ;
148- const baseProjection = this . _getMapProjection ( mapInfo ) ;
149- // 坐标系异常处理
150- if ( ! ( baseProjection in projectionMap ) ) {
151- throw Error ( Lang . i18n ( 'msg_crsunsupport' ) ) ;
152- }
153- this . webMapInstance = this . _initMap ( mapInfo ) ;
154- this . _registerWebMapEvents ( ) ;
155- this . webMapInstance . createWebMap ( mapInfo ) ;
156- } )
157- . catch ( ( error ) => {
158- /**
159- * @event WebMap#getmapfailed
160- * @description 获取地图信息失败。
161- * @property {Object } error - 失败原因。
162- */
163- this . fire ( 'getmapfailed' , { error : error } ) ;
164- } ) ;
165- }
138+ FetchRequest . get ( mapUrl , null , { withCredentials : this . options . withCredentials } )
139+ . then ( ( response ) => {
140+ return response . json ( ) ;
141+ } )
142+ . then ( ( mapInfo ) => {
143+ const projectionMap = [ 'EPSG:4490' , 'EPSG:4214' , 'EPSG:4610' , 'EPSG:3857' , 'EPSG:4326' ] ;
144+ const baseProjection = this . _getMapProjection ( mapInfo ) ;
145+ // 坐标系异常处理
146+ if ( ! projectionMap . find ( item => item === baseProjection ) ) {
147+ throw Error ( Lang . i18n ( 'msg_crsunsupport' ) ) ;
148+ }
149+ this . webMapInstance = this . _initMap ( mapInfo ) ;
150+ this . _registerWebMapEvents ( ) ;
151+ this . webMapInstance . initializeMap ( mapInfo ) ;
152+ } )
153+ . catch ( ( error ) => {
154+ /**
155+ * @event WebMap#getmapfailed
156+ * @description 获取地图信息失败。
157+ * @property {Object } error - 失败原因。
158+ */
159+ this . fire ( 'getmapfailed' , { error : error } ) ;
160+ } ) ;
161+ }
166162
167- /**
168- * @private
169- * @function WebMap.prototype._getMapProjection
170- * @param {object } mapInfo - 地图信息。
171- * @description 获取地图投影。
172- */
173- _getMapProjection ( mapInfo ) {
174- if ( this . _isWebMapV3 ( mapInfo . version ) ) {
175- return mapInfo . crs ;
176- }
177- return mapInfo . projection ;
178- }
163+ /**
164+ * @private
165+ * @function WebMap.prototype._getMapProjection
166+ * @param {object } mapInfo - 地图信息。
167+ * @description 获取地图投影。
168+ */
169+ _getMapProjection ( mapInfo ) {
170+ if ( this . _isWebMapV3 ( mapInfo . version ) ) {
171+ return mapInfo . crs ;
172+ }
173+ return mapInfo . projection ;
174+ }
179175
180176 /**
181177 * @private
@@ -184,34 +180,41 @@ export class WebMap extends mapboxgl.Evented {
184180 * @description 初始化 WebMap 实例
185181 */
186182 _initMap ( mapInfo ) {
187- const WebMapFactory = this . _isWebMapV3 ( mapInfo . version ) ? WebMapV3 : WebMapV2 ;
188- const webMapInstance = new WebMapFactory ( this . mapId , this . options , this . mapOptions ) ;
189- webMapInstance . setEventedParent ( this ) ;
190- return webMapInstance ;
191- }
183+ const WebMap = this . _getMapFactory ( mapInfo . version ) ;
184+ const webMapInstance = new WebMap ( this . mapId , this . options , this . mapOptions ) ;
185+ webMapInstance . setEventedParent ( this ) ;
186+ return webMapInstance ;
187+ }
188+
189+ _getMapFactory ( version ) {
190+ if ( this . _isWebMapV3 ( version ) ) {
191+ return WebMapV3 ;
192+ }
193+ return WebMapV2 ;
194+ }
192195
193196 /**
194197 * @private
195198 * @function WebMap.prototype._registerWebMapEvents
196199 * @description 注册 WebMap 事件
197200 */
198201 _registerWebMapEvents ( ) {
199- if ( ! this . webMapInstance ) {
200- return ;
201- }
202- this . webMapInstance . on ( 'mapinitialized' , ( ) => {
203- this . map = this . webMapInstance . map ;
204- } ) ;
205- this . webMapInstance . on ( 'addlayerssucceeded' , ( { mapparams } ) => {
206- this . mapParams = mapparams ;
207- } ) ;
208- }
202+ if ( ! this . webMapInstance ) {
203+ return ;
204+ }
205+ this . webMapInstance . on ( 'mapinitialized' , ( ) => {
206+ this . map = this . webMapInstance . map ;
207+ } ) ;
208+ this . webMapInstance . on ( 'addlayerssucceeded' , ( { mapparams } ) => {
209+ this . mapParams = mapparams ;
210+ } ) ;
211+ }
209212
210- _getWebMapInstance ( ) {
211- return this . webMapInstance ;
212- }
213+ _getWebMapInstance ( ) {
214+ return this . webMapInstance ;
215+ }
213216
214- _isWebMapV3 ( version ) {
217+ _isWebMapV3 ( version ) {
215218 return version . startsWith ( '3.' ) ;
216- }
217- }
219+ }
220+ }
0 commit comments