11import { MapService } from '../services/MapService' ;
2- import { InitMapServiceBase , isPlaneProjection } from '@supermap/iclient-common/iServer/InitMapServiceBase' ;
2+ import { InitMapServiceBase , isPlaneProjection , getTileFormat , getTileset } from '@supermap/iclient-common/iServer/InitMapServiceBase' ;
33import olMap from 'ol/Map' ;
44import View from 'ol/View' ;
55import Tile from 'ol/layer/Tile' ;
@@ -41,29 +41,53 @@ window.proj4 = proj4;
4141 * initMap(url, { mapOptions, viewOptions, layerOptions, sourceOptions })
4242 * ```
4343 * */
44- export function initMap ( url , options = { } ) {
44+ export async function initMap ( url , options = { } ) {
4545 const { mapOptions, viewOptions, layerOptions, sourceOptions } = options ;
4646 const initMapService = new InitMapServiceBase ( MapService , url , options ) ;
47- return initMapService . getMapInfo ( async function ( serviceResult , resolve , reject ) {
48- if ( ! serviceResult || ! serviceResult . result ) {
49- reject ( 'service is not work!' ) ;
50- return ;
51- }
52- let { prjCoordSys, bounds } = serviceResult . result ;
53- if ( ! get ( `EPSG:${ prjCoordSys . epsgCode } ` ) && ! isPlaneProjection ( prjCoordSys . type ) ) {
54- const wkt = await initMapService . getWKT ( ) ;
55- registerProj ( prjCoordSys . epsgCode , wkt , bounds ) ;
56- }
47+ const tilesets = await initMapService . getTilesets ( ) ;
48+ const result = await initMapService . getMapInfo ( ) ;
49+ if ( ! result || ! result . result ) {
50+ return 'service is not work!' ;
51+ }
52+ const mapObject = result . result ;
53+ const { prjCoordSys, bounds, center, visibleScales, coordUnit, scale, dpi } = mapObject ;
54+ // tileset和地图不同投影,优先使用地图
55+ const tileset = getTileset ( tilesets . result , { prjCoordSys, tileType : 'Image' } ) ;
5756
58- let map = createMap ( serviceResult . result , mapOptions , viewOptions ) ;
59- let { layer, source } = createLayer ( url , serviceResult . result , sourceOptions , layerOptions ) ;
60- map . addLayer ( layer ) ;
61- resolve ( {
62- map,
63- source,
64- layer
65- } ) ;
66- } ) ;
57+ const config = {
58+ center,
59+ bounds,
60+ dpi,
61+ visibleScales,
62+ scale,
63+ prjCoordSys,
64+ coordUnit,
65+ tileFormat : 'webp' ,
66+ tileSize : 256
67+ } ;
68+ if ( tileset ) {
69+ config . tileFormat = getTileFormat ( tileset ) ;
70+ config . tileSize = tileset . tileWidth || 256 ;
71+ config . transparent = tileset . transparent || true ;
72+ config . origin = [ tileset . originalPoint . x , tileset . originalPoint . y ] ;
73+ config . resolutions = tileset . resolutions ;
74+ config . scaleDenominators = tileset . scaleDenominators ;
75+ config . dpi = Util . getDpi ( 1.0 / tileset . scaleDenominators [ 0 ] , tileset . resolutions [ 0 ] , coordUnit ) ;
76+ }
77+
78+ if ( ! get ( `EPSG:${ prjCoordSys . epsgCode } ` ) && ! isPlaneProjection ( prjCoordSys . type ) ) {
79+ const wkt = await initMapService . getWKT ( ) ;
80+ registerProj ( prjCoordSys . epsgCode , wkt , bounds ) ;
81+ }
82+
83+ let map = createMap ( config , mapOptions , viewOptions ) ;
84+ let { layer, source } = createLayer ( url , config , sourceOptions , layerOptions ) ;
85+ map . addLayer ( layer ) ;
86+ return {
87+ map,
88+ source,
89+ layer
90+ } ;
6791}
6892
6993/**
@@ -77,13 +101,14 @@ export function initMap(url, options = {}) {
77101 */
78102
79103export function viewOptionsFromMapJSON ( mapJSONObj , level = 22 ) {
80- let { bounds, dpi, center, visibleScales, scale, coordUnit, prjCoordSys } = mapJSONObj ;
104+ let { bounds, dpi, center, visibleScales, scale, coordUnit, prjCoordSys, scaleDenominators , resolutions } = mapJSONObj ;
81105 const mapCenter =
82106 center . x && center . y ? [ center . x , center . y ] : [ ( bounds . left + bounds . right ) / 2 , ( bounds . bottom + bounds . top ) / 2 ] ;
83107 const extent = [ bounds . left , bounds . bottom , bounds . right , bounds . top ] ;
84108 let projection = Util . getProjection ( prjCoordSys , extent ) ;
85- var resolutions = Util . scalesToResolutions ( visibleScales , bounds , dpi , coordUnit , level ) ;
86- const zoom = Util . getZoomByResolution ( Util . scaleToResolution ( scale , dpi , coordUnit ) , resolutions ) ;
109+ resolutions = resolutions || Util . scalesToResolutions ( visibleScales , bounds , dpi , coordUnit , level ) ;
110+ const resolution = scaleDenominators ? ( 1.0 / scale ) : Util . scaleToResolution ( scale , dpi , coordUnit ) ;
111+ const zoom = Util . getZoomByResolution ( resolution , resolutions ) ;
87112 return {
88113 center : mapCenter ,
89114 projection,
0 commit comments