(url, options)
| 49 | * ``` |
| 50 | */ |
| 51 | export async function initMap(url, options) { |
| 52 | options = options || {}; |
| 53 | const mapOptions = options.mapOptions || {}; |
| 54 | const layerOptions = options.layerOptions || {}; |
| 55 | const initMapService = new InitMapServiceBase(MapService, url, options); |
| 56 | |
| 57 | const tilesets = await initMapService.getTilesets(); |
| 58 | const result = await initMapService.getMapInfo(); |
| 59 | const mapObject = result.result; |
| 60 | const prjCoordSys = mapObject.prjCoordSys; |
| 61 | // tileset和地图不同投影,优先使用地图 |
| 62 | const tileset = getTileset(tilesets.result, { prjCoordSys, tileType: 'Image' }); |
| 63 | const bounds = mapObject.bounds; |
| 64 | const center = mapObject.center; |
| 65 | const scale = mapObject.scale; |
| 66 | if (!isPlaneProjection(prjCoordSys.type)) { |
| 67 | const epsgCode = getEpsgCode(prjCoordSys); |
| 68 | await setProj(epsgCode, initMapService); |
| 69 | } |
| 70 | let crs, zoom, maxZoom, tileFormat, tileSize, transparent; |
| 71 | if (tileset) { |
| 72 | tileFormat = getTileFormat(tileset); |
| 73 | tileSize = tileset.tileWidth || 256; |
| 74 | transparent = tileset.transparent || true; |
| 75 | const origin = [tileset.originalPoint.x, tileset.originalPoint.y] |
| 76 | const resolutions = tileset.resolutions; |
| 77 | const scaleDenominators = tileset.scaleDenominators |
| 78 | const coordUnit = mapObject.coordUnit; |
| 79 | maxZoom = resolutions.length - 1; |
| 80 | crs = crsFromMapJSON({ prjCoordSys, bounds, resolutions, origin, dpi: getDpi(1.0 / scaleDenominators[0], resolutions[0], coordUnit) }, { maxZoom }); |
| 81 | zoom = getZoomByResolution(1.0 / scale, scaleDenominators); |
| 82 | } else { |
| 83 | tileFormat = 'webp'; |
| 84 | tileSize = 256; |
| 85 | transparent = true; |
| 86 | const { scale, dpi, coordUnit } = mapObject; |
| 87 | const origin = [bounds.left, bounds.top]; |
| 88 | const resolutions = scalesToResolutions(mapObject.visibleScales, bounds, dpi, coordUnit, mapOptions.maxZoom, scale); |
| 89 | maxZoom = resolutions.length - 1; |
| 90 | crs = crsFromMapJSON({ prjCoordSys, bounds, resolutions, origin, dpi }, { maxZoom }); |
| 91 | zoom = getZoomByScale({ scale, dpi, coordUnit }, resolutions); |
| 92 | } |
| 93 | |
| 94 | const mapInfoOptions = { |
| 95 | crs, |
| 96 | center: getCenter(crs, { center, bounds }), |
| 97 | zoom, |
| 98 | maxZoom |
| 99 | }; |
| 100 | const map = L.map('map', { ...mapInfoOptions, ...mapOptions }); |
| 101 | const layer = new TiledMapLayer(url, { |
| 102 | ...{ |
| 103 | noWrap: true, |
| 104 | format: tileFormat, |
| 105 | minZoom: 0, |
| 106 | maxZoom, |
| 107 | tileSize, |
| 108 | bounds: getLatLngBounds(bounds, crs), |
no test coverage detected