-
Notifications
You must be signed in to change notification settings - Fork 0
/
olmap.html
229 lines (206 loc) · 7.89 KB
/
olmap.html
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<style>
.map {
height: 400px;
width: 100%;
}
</style>
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<title>OpenLayers example</title>
</head>
<body>
<h2>My Map</h2>
<div id="map" class="map"></div>
<script type="text/javascript">
var projection = ol.proj.get("EPSG:3857");
var projectionExtent = projection.getExtent();
var resolutions = [];
for (var i = 0; i < 19; i++) {
resolutions[i] = Math.pow(2, 18 - i);
}
var tilegrid = new ol.tilegrid.TileGrid({
origin: ol.extent.getBottomLeft(projectionExtent),
resolutions: resolutions
});
var tileExtent = function(tileCoord){
var z = tileCoord[0];
var x = tileCoord[1];
var y = tileCoord[2];
var tileGridOrigin = tilegrid.getOrigin(z);
var tileSizeAtResolution = tilegrid.getTileSize(z) * tilegrid.getResolution(z);
return [
tileGridOrigin[0] + tileSizeAtResolution * x,
tileGridOrigin[1] + tileSizeAtResolution * y,
tileGridOrigin[0] + tileSizeAtResolution * (x + 1),
tileGridOrigin[1] + tileSizeAtResolution * (y + 1)
];
}
function long2tile(lon,zoom1) {
tt = Number(lon);
return (Math.floor((tt+180)/360*Math.pow(2,zoom1)));
}
function lat2tile(lat,zoom2) {
return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom2)));
}
function tile2long(x,z) {
return (x/Math.pow(2,z)*360-180);
}
function tile2lat(y,z) {
var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}
function inside(point, vs) {
// ray-casting algorithm based on
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1];
var xj = vs[j][0], yj = vs[j][1];
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};
var polygon = [ [ 87, 48 ], [ 73, 39 ], [ 82, 29 ], [ 98, 27 ], [ 100, 22], [117, 24], [121, 31], [ 134 , 48], [ 120 , 53 ], [ 104 , 41] ];
var baidu_source = new ol.source.TileImage({
projection: projection,
tileGrid: tilegrid,
opaque: false,
tileUrlFunction: function (tileCoord, pixelRatio, proj) {
if (!tileCoord) {
return "";
}
var z = tileCoord[0];
var x = tileCoord[1];
var y = tileCoord[2];
if (x < 0) {
x = "M" + (-x);
}
if (y < 0) {
y = "M" + (-y);
}
return "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=20170809&scaler=1&p=1";
}
});
var baiduMapLayer = new ol.layer.Tile({
source: baidu_source
});
var amapMapLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
// url: 'https://grid.amap.com/grid/{z}/{x}/{y}?src=jsapi&key=&lang=en&dpiType=webrd',
tileSize: 512,
opaque: false,
minZoom: 3,
tileUrlFunction: function (tileCoord) {
if (!tileCoord) {
return "";
}
var z = tileCoord[0];
var x = tileCoord[1];
var y = tileCoord[2];
var url;
if (x < 0) {
x = (-x) - 1;
}
if (y < 0) {
y = (-y) - 1;
}
var lon = tile2long(x, z);
var lan = tile2lat(y, z);
if (z >= 10) {
if (inside([ lon, lan], polygon)) {
url = "http://wprd01.is.autonavi.com/appmaptile?lang=en&size=1&style=7&x=" + x + "&y=" + y + "&z=" + z;
}
else {
url = "https://grid.amap.com/grid/" + z + "/" + x + "/" + y +"?src=jsapi&key=&lang=en&dpiType=webrd";
}
}
else {
url = "http://wprd01.is.autonavi.com/appmaptile?lang=en&size=1&style=7&x=" + x + "&y=" + y + "&z=" + z;
}
console.log("tileUrl:" + url);
console.log("lon:" + lon + ", lan:" + lan);
console.log("inside:" + inside([ lon, lan], polygon));
// a = tileExtent(tileCoord);
// console.log(a[0], a[1]);
return url;
//return "https://grid.amap.com/grid/{z}/{x}/{y}?src=jsapi&key=&lang=en&dpiType=webrd";
}
})
})
var amapWebLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
// url: 'https://grid.amap.com/grid/{z}/{x}/{y}?src=jsapi&key=&lang=en&dpiType=webrd',
tileSize: 512,
opaque: false,
minZoom: 3,
tileUrlFunction: function (tileCoord) {
if (!tileCoord) {
return "";
}
var z = tileCoord[0];
var x = tileCoord[1];
var y = tileCoord[2];
var url;
if (x < 0) {
x = (-x) - 1;
}
if (y < 0) {
y = (-y) - 1;
}
var lon = tile2long(x, z);
var lan = tile2lat(y, z);
// if (z >= 10) {
// url = "https://grid.amap.com/grid/" + z + "/" + x + "/" + y +"?src=jsapi&key=&lang=en&dpiType=webrd";
// }
// else {
// url = "http://wprd01.is.autonavi.com/appmaptile?lang=en&size=1&style=7&x=" + x + "&y=" + y + "&z=" + z;
// }
// console.log("tileUrl:" + url);
// console.log("lon:" + lon + ", lan:" + lan);
// a = tileExtent(tileCoord);
// console.log(a[0], a[1]);
return "https://restapi.amap.com/v3/staticmap?location=" + lon + "," + lan + "&zoom="+ z + "&size=250*250&key=8e5c01b860f41d0a3eb793e86300e8a2";
//return "https://grid.amap.com/grid/{z}/{x}/{y}?src=jsapi&key=&lang=en&dpiType=webrd";
}
})
})
var AmapGridLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'https://grid.amap.com/grid/{z}/{x}/{y}?src=jsapi&key=&lang=en&dpiType=webrd',
tileSize: 512,
opaque: false,
minZoom: 3,
})
});
var AmapLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=en&size=1&style=7&x={x}&y={y}&z={z}',
tileSize: 512,
opaque: false,
minZoom: 3,
})
});
var map = new ol.Map({
target: 'map',
layers: [
//baiduMapLayer,
// AmapGridLayer,
// AmapLayer
amapMapLayer
// amapWebLayer
],
// layers: [baiduMapLayer],
view: new ol.View({
center: ol.proj.fromLonLat([37.41, 8.82]),
zoom: 4
})
});
</script>
</body>
</html>