// import OMAP from './OMAP-source.js'
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js'
import XYZ from 'ol/source/XYZ'
import Map from 'ol/Map'
import View from 'ol/View'
import TileImage from 'ol/source/TileImage'
import WMTSTileGrid from 'ol/tilegrid/WMTS'
function zeroPad(num, len, radix) {
    var str = num.toString(radix || 10);
    while (str.length < len) {
        str = "0" + str;
    }
    return str;
}
export function init3D(){
    var baiduMapLayer = new TileLayer({
        source: new XYZ({
            tilePixelRatio: 2,    //瓦片像素比
            tileUrlFunction: function (tileCoord) {  // 参数tileCoord为瓦片坐标
                var z = tileCoord[0];
                var x = tileCoord[1];
                var y = tileCoord[2];
                // 计算当前层级下瓦片总数的一半，用于定位整个地图的中心点
                var halfTileNum = Math.pow(2, z - 1);
                // 原点移到中心点后，计算xy方向上新的坐标位置
                var baiduX = x - halfTileNum;
                var baiduY = y + halfTileNum;
                // 百度瓦片服务url将负数使用M前缀来标识
                if (baiduX < 0) {
                    baiduX = 'M' + (-baiduX);
                }
                if (baiduY < 0) {
                    baiduY = 'M' + (-baiduY);
                }
                // 返回经过转换后，对应于百度在线瓦片的url
                return 'http://online2.map.bdimg.com/onlinelabel/?qt=tile&x=' + baiduX + '&y=' + baiduY + '&z=' + z + '&styles=pl&udt=20160321&scaler=2&p=0';
            }
        })
    });
    // 创建地图
    var map = new Map({
        layers: [
            baiduMapLayer
        ],
        view: new View({
            center: [121.29949364962295,31.18793438654],
            projection: 'EPSG:4326',
            zoom: 4
        }),
        target: 'baiduMap'
    });
    // // 坐标原点
    var origin = [-400.0, 400];
    // 瓦片网格
    var tileGrid = new WMTSTileGrid({
        tileSize: 256,
        origin: origin,
        resolutions: [
                0.7031250000029744, 0.3515625000014872, 0.1757812499888463,
                0.08789062499442316,0.04394531250910888,0.021972656242657134,
                0.010986328121328567, 0.00549316407256159,0.0027465820243834896,
                0.0013732910240890498,6.866455120445249E-4,3.433227441249574E-4,
                1.7166138395978374E-4,8.583068008258684E-5,4.291534004129342E-5,
                2.145767002064671E-5,9.658089455004757E-6, 5.364423453814192E-6,
                2.682199829602067E-6,1.3411118121060625E-6
            ]
    });
    //标准TMS切片加载方式
    var offlineSource = new TileImage({
        projection: 'EPSG:4326',//注意这里要写成你下载的地图资源使用的投影坐标系
        // 瓦片网格
        tileGrid: tileGrid,
        tileUrlFunction: function (tileCoord, pixelRatio, proj) {
            // 缩放级别
            var z = zeroPad(tileCoord[0], 2, 10);
            // 瓦片行号
            var x = zeroPad(tileCoord[1], 8, 16);
            // 瓦片列号
            var y = zeroPad(-tileCoord[2] - 1, 8, 16);
            // 瓦片本地路径
            return "TestMapTile/" + "L" + z + "/" + "R" + y + "/" + "C" + x + ".png";
        }
    });
    var offlineMapLayer = new TileLayer({
        source: offlineSource
    });
    //map.addLayer(offlineMapLayer);
}