forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaiduMap.js
More file actions
120 lines (114 loc) · 4.6 KB
/
BaiduMap.js
File metadata and controls
120 lines (114 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import TileImage from 'ol/source/TileImage';
import * as asserts from 'ol/asserts';
import TileGrid from 'ol/tilegrid/TileGrid';
import { Util } from '../core/Util';
/**
* @class BaiduMap
* @browsernamespace ol.source
* @category ThirdPartyMap
* @classdesc 百度地图图层源。
* @modulecategory Mapping
* @param {Object} opt_options - 参数。
* @param {string} [opt_options.url='https://maponline{num}.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles={styles}&udt=20170408'] - 服务地址。
* @param {string} [opt_options.tileProxy] - 代理地址。
* @param {boolean} [hidpi = false] - 是否使用高分辨率地图。
* @extends {ol.source.TileImage}
* @usage
*/
export class BaiduMap extends TileImage {
constructor(opt_options) {
var options = opt_options || {};
var attributions =
options.attributions ||
"Map Data © 2018 Baidu - GS(2016)2089号 - Data © 长地万方 with <span>© SuperMap iClient</span>";
var tileGrid = BaiduMap.defaultTileGrid();
var crossOrigin = options.crossOrigin !== undefined ? options.crossOrigin : 'anonymous';
var url =
options.url !== undefined
? options.url
: 'https://maponline{num}.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles={styles}&udt=20170408';
var hidpi =
options.hidpi || (window.devicePixelRatio || window.screen.deviceXDPI / window.screen.logicalXDPI) > 1;
url = url.replace('{styles}', hidpi ? 'ph' : 'pl');
super({
attributions: attributions,
cacheSize: options.cacheSize,
crossOrigin: crossOrigin,
opaque: options.opaque !== undefined ? options.opaque : true,
maxZoom: options.maxZoom !== undefined ? options.maxZoom : 19,
reprojectionErrorThreshold: options.reprojectionErrorThreshold,
tileLoadFunction: options.tileLoadFunction,
projection: 'EPSG:3857',
wrapX: options.wrapX,
tilePixelRatio: hidpi ? 2 : 1,
tileGrid: tileGrid,
tileUrlFunction: tileUrlFunction
});
if (options.tileProxy) {
this.tileProxy = options.tileProxy;
}
var me = this;
// eslint-disable-next-line no-unused-vars
function tileUrlFunction(tileCoord, pixelRatio, projection) {
var tempUrl = url
.replace('{num}', Math.abs((tileCoord[1] + tileCoord[2]) % 4))
.replace('{z}', tileCoord[0].toString())
.replace('{x}', tileCoord[1].toString())
.replace('{y}', function() {
var y = ['4', '5'].indexOf(Util.getOlVersion()) > -1 ? tileCoord[2] : -tileCoord[2] - 1;
return y.toString();
})
.replace('{-y}', function() {
var z = tileCoord[0];
var range = tileGrid.getFullTileRange(z);
asserts.assert(range, 55); // The {-y} placeholder requires a tile grid with extent
var y = range.getHeight() + tileCoord[2];
return y.toString();
});
//支持代理
if (me.tileProxy) {
tempUrl = me.tileProxy + encodeURIComponent(tempUrl);
}
return tempUrl;
}
}
// TODO 确认这个方法是否要开出去
/**
* @function BaiduMap.defaultTileGrid
* @description 获取默认瓦片格网。
* @returns {ol.tilegrid.TileGrid} 返回瓦片格网对象。
*/
static defaultTileGrid() {
var tileGird = new TileGrid({
extent: [-33554432, -33554432, 33554432, 33554432],
resolutions: [
131072 * 2,
131072,
65536,
32768,
16284,
8192,
4096,
2048,
1024,
512,
256,
128,
64,
32,
16,
8,
4,
2,
1,
0.5
],
origin: [0, 0],
minZoom: 3
});
return tileGird;
}
}