Skip to content

Commit 15fc593

Browse files
committed
fix 漏提交
1 parent cd52ef4 commit 15fc593

File tree

4 files changed

+705
-0
lines changed

4 files changed

+705
-0
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
6+
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
7+
<title data-i18n="resources.title_graphicLayer_webgl"></title>
8+
9+
<style>
10+
11+
#title {
12+
position: absolute;
13+
color: white;
14+
left: 0;
15+
top: 30px;
16+
text-align: center;
17+
width: 100%;
18+
}
19+
20+
#title > h3 {
21+
margin: 10px 0;
22+
letter-spacing: 0.1em;
23+
}
24+
25+
#title > h6 {
26+
margin: 0;
27+
font-weight: normal;
28+
}
29+
30+
.ol-popup {
31+
position: absolute;
32+
background-color: white;
33+
-webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
34+
filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
35+
padding: 15px;
36+
border-radius: 10px;
37+
border: 1px solid #cccccc;
38+
bottom: 12px;
39+
left: -50px;
40+
min-width: 50px;
41+
}
42+
43+
.ol-popup:after, .ol-popup:before {
44+
top: 100%;
45+
border: solid transparent;
46+
content: " ";
47+
height: 0;
48+
width: 0;
49+
position: absolute;
50+
pointer-events: none;
51+
}
52+
53+
.ol-popup:after {
54+
border-top-color: white;
55+
border-width: 10px;
56+
left: 48px;
57+
margin-left: -10px;
58+
}
59+
60+
.ol-popup:before {
61+
border-top-color: #cccccc;
62+
border-width: 11px;
63+
left: 48px;
64+
margin-left: -11px;
65+
}
66+
67+
.ol-panel {
68+
position: absolute;
69+
top: .5em;
70+
right:0;
71+
z-index: 10;
72+
}
73+
74+
</style>
75+
</head>
76+
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;">
77+
<div id="map" style="width: 100%;height:100%"></div>
78+
79+
<div id="popup" class="ol-popup">
80+
<div id="popup-content"></div>
81+
</div>
82+
<div id="title">
83+
<h3 data-i18n="resources.text_graphicLayer_title"></h3>
84+
<h6 data-i18n="resources.text_graphicLayer_subTitle"></h6>
85+
</div>
86+
<div id="control" class="ol-panel"></div>
87+
<script type="text/javascript" include="papaparse,dat-gui,widgets" src="../js/include-web.js"></script>
88+
<script type="text/javascript" include="deck" src="../../dist/include-openlayers.js"></script>
89+
<script type="text/javascript">
90+
var url = (window.isLocal ? window.server : "http://117.122.248.69:8090") + "/iserver/services/map-china400/rest/maps/ChinaDark";
91+
92+
var map, overlay, graphicLayer;
93+
94+
new ol.supermap.MapService(url).getMapInfo(function (serviceResult) {
95+
var mapJSONObj = serviceResult.result;
96+
var container = document.getElementById('popup');
97+
overlay = new ol.Overlay(({
98+
element: container,
99+
autoPan: true,
100+
autoPanAnimation: {
101+
duration: 250
102+
}
103+
}));
104+
map = new ol.Map({
105+
target: 'map',
106+
controls: ol.control.defaults({attributionOptions: {collapsed: false}})
107+
.extend([new ol.supermap.control.Logo()]),
108+
view: new ol.View({
109+
center: ol.proj.transform([-73.9286, 40.75], 'EPSG:4326', 'EPSG:3857'),
110+
zoom: 11,
111+
projection: 'EPSG:3857'
112+
}),
113+
overlays: [overlay]
114+
});
115+
var options = ol.source.TileSuperMapRest.optionsFromMapJSON(url, mapJSONObj);
116+
var layer = new ol.layer.Tile({
117+
source: new ol.source.TileSuperMapRest(options)
118+
});
119+
map.addLayer(layer);
120+
// var raster = new ol.layer.Tile({
121+
// source: new ol.source.OSM()
122+
// });
123+
// map.addLayer(raster);
124+
125+
widgets.loader.showLoader("data loading...");
126+
Papa.parse('../data/nyc-taxi.csv', {
127+
download: true,
128+
skipEmptyLines: true,
129+
header: true,
130+
error: function () {
131+
widgets.loader.removeLoader();
132+
alert("parse error")
133+
},
134+
complete: function (results) {
135+
widgets.loader.removeLoader();
136+
addGraphicLayer(results.data);
137+
}
138+
});
139+
140+
});
141+
142+
function addGraphicLayer(data) {
143+
var graphics = [], count = data.length;
144+
145+
for (var i = 0; i < count; ++i) {
146+
var coods = data[i];
147+
var coordinates = [parseFloat(coods.lng), parseFloat(coods.lat)];
148+
if (coordinates[0] === coordinates[1]) {
149+
continue;
150+
}
151+
coordinates = ol.proj.transform(coordinates, 'EPSG:4326', 'EPSG:3857');
152+
graphics[i] = new ol.Graphic(new ol.geom.Point(coordinates));
153+
}
154+
var graphicStyle = {
155+
color: [0, 255, 128, 255],
156+
highlightColor: [255, 0, 0, 255],
157+
radius: 20
158+
};
159+
map.once('postrender', function () {
160+
var content = document.getElementById('popup-content');
161+
graphicLayer = new ol.layer.Image({
162+
source: new ol.source.Graphic({
163+
graphics: graphics,
164+
renderer: "webgl",
165+
color: graphicStyle.color,
166+
highlightColor: graphicStyle.highlightColor,
167+
radius: graphicStyle.radius,
168+
169+
map: map,
170+
onClick: function (graphic) {
171+
if (graphic) {
172+
var coords = graphic.lngLat;
173+
content.innerHTML = resources.text_coordinate + ":[" + coords[0] + "," + coords[1] + "]";
174+
overlay.setPosition(ol.proj.transform(coords, 'EPSG:4326', 'EPSG:3857'));
175+
return;
176+
}
177+
overlay.setPosition(undefined);
178+
}
179+
})
180+
});
181+
map.addLayer(graphicLayer);
182+
});
183+
initDatGui(graphicStyle)
184+
}
185+
186+
//设置面板
187+
function initDatGui(options) {
188+
189+
var gui = new dat.GUI();
190+
191+
var popup = document.getElementById('control');
192+
popup.appendChild(gui.domElement);
193+
var control = new ol.control.Control({element: popup});
194+
control.setMap(map);
195+
map.addControl(control);
196+
197+
gui.addColor(options, 'color').onChange(finished);
198+
gui.add(options, 'radius', 0, 100).onChange(finished);
199+
200+
function finished() {
201+
graphicLayer.getSource().setStyle(options);
202+
}
203+
}
204+
205+
</script>
206+
</body>
207+
</html>
14 KB
Loading
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import ol from "openlayers";
2+
import {CommonUtil} from "@supermap/iclient-common";
3+
import {Util} from '../../core/Util';
4+
5+
//获取某像素坐标点pixelP绕中心center逆时针旋转rotation弧度后的像素点坐标。
6+
function rotate(pixelP, rotation, center) {
7+
let x = Math.cos(rotation) * (pixelP[0] - center[0]) - Math.sin(rotation) * (pixelP[1] - center[1]) + center[0];
8+
let y = Math.sin(rotation) * (pixelP[0] - center[0]) + Math.cos(rotation) * (pixelP[1] - center[1]) + center[1];
9+
return [x, y];
10+
}
11+
12+
//获取某像素坐标点pixelP相对于中心center进行缩放scaleRatio倍后的像素点坐标。
13+
function scale(pixelP, center, scaleRatio) {
14+
let x = (pixelP[0] - center[0]) * scaleRatio + center[0];
15+
let y = (pixelP[1] - center[1]) * scaleRatio + center[1];
16+
return [x, y];
17+
}
18+
19+
/**
20+
* @private
21+
* @class GraphicCanvasRenderer
22+
* @classdesc 高效率点图层 canvas渲染器。
23+
* @category Visualization Graphic
24+
* @extends L.Layer{@linkdoc-leaflet/#layer}
25+
* @param layer - {Array<L.supermap.graphicLayer>} 高效率点图层
26+
* @param {Object} options - 图层参数。
27+
* @param {number} options.width - 地图宽度
28+
* @param {number} options.height - 地图高度
29+
* @param {HTMLElement} options.container - 放置渲染器的父元素
30+
*
31+
* @param {Array<number>} options.color - 颜色,目前只支持rgba数组。默认[0, 0, 0, 255],
32+
* @param {number} options.radius - 半径,默认10
33+
* @param {number} options.opacity - 不透明度,默认0.8
34+
* @param {Array} options.highlightColor - 高亮颜色,目前只支持rgba数组
35+
* @param {number} options.radiusScale - 点放大倍数
36+
* @param {number} options.radiusMinPixels - 半径最小值(像素)
37+
* @param {number} options.radiusMaxPixels - 半径最大值(像素)
38+
* @param {number} options.strokeWidth - 边框大小
39+
* @param {boolean} options.outline - 是否显示边框
40+
*
41+
* @param {function} options.onClick - 点击事件
42+
* @param {function} options.onHover - 悬停事件
43+
*/
44+
export class GraphicCanvasRenderer extends ol.Object {
45+
constructor(layer, options) {
46+
super();
47+
this.layer = layer;
48+
this.map = layer.map;
49+
let opt = options || {};
50+
CommonUtil.extend(this, opt);
51+
this.highLightStyle = this.layer.highLightStyle;
52+
53+
let _pixelRatio = this.pixelRatio;
54+
let _size = this.size;
55+
56+
this.mapWidth = _size[0] * _pixelRatio;
57+
this.mapHeight = _size[1] * _pixelRatio;
58+
this.width = this.map.getSize()[0] * _pixelRatio;
59+
this.height = this.map.getSize()[1] * _pixelRatio;
60+
61+
this.context = Util.createCanvasContext2D(this.mapWidth, this.mapHeight);
62+
this.canvas = this.context.canvas;
63+
}
64+
65+
update() {
66+
this.layer.changed();
67+
}
68+
69+
_clearBuffer() {
70+
}
71+
72+
73+
/**
74+
* @private
75+
* @function GraphicCanvasRenderer.prototype.getCanvas
76+
* @description 返回画布
77+
* @return {HTMLCanvasElement} canvas对象
78+
*/
79+
getCanvas() {
80+
return this.canvas;
81+
}
82+
83+
/**
84+
* @private
85+
* @function GraphicCanvasRenderer.prototype.drawGraphics
86+
* @description 绘制点要素
87+
*/
88+
drawGraphics(graphics) {
89+
this.graphics_ = graphics || [];
90+
91+
let pixelRatio = this.pixelRatio;
92+
let mapWidth = this.mapWidth;
93+
let mapHeight = this.mapHeight;
94+
let width = this.width;
95+
let height = this.height;
96+
97+
let offset = [(mapWidth - width) / 2 / pixelRatio, (mapHeight - height) / 2 / pixelRatio];
98+
let vectorContext = ol.render.toContext(this.context, {
99+
size: [mapWidth, mapHeight],
100+
pixelRatio: 1
101+
});
102+
103+
let me = this, layer = me.layer, map = layer.map;
104+
graphics.map(function (graphic) {
105+
let style = graphic.getStyle();
106+
if (me.selected === graphic) {
107+
let defaultHighLightStyle = style;
108+
if (style instanceof ol.style.Circle) {
109+
defaultHighLightStyle = new ol.style.Circle({
110+
radius: style.getRadius(),
111+
fill: new ol.style.Fill({
112+
color: 'rgba(0, 153, 255, 1)'
113+
}),
114+
stroke: style.getStroke(),
115+
snapToPixel: style.getSnapToPixel()
116+
});
117+
} else if (style instanceof ol.style.RegularShape) {
118+
defaultHighLightStyle = new ol.style.RegularShape({
119+
radius: style.getRadius(),
120+
radius2: style.getRadius2(),
121+
points: style.getPoints(),
122+
angle: style.getAngle(),
123+
snapToPixel: style.getSnapToPixel(),
124+
rotation: style.getRotation(),
125+
rotateWithView: style.getRotateWithView(),
126+
fill: new ol.style.Fill({
127+
color: 'rgba(0, 153, 255, 1)'
128+
}),
129+
stroke: style.getStroke()
130+
});
131+
}
132+
style = me.highLightStyle || defaultHighLightStyle;
133+
}
134+
vectorContext.setStyle(new ol.style.Style({
135+
image: style
136+
}));
137+
let geometry = graphic.getGeometry();
138+
let coordinate = geometry.getCoordinates();
139+
let pixelP = map.getPixelFromCoordinate(coordinate);
140+
let rotation = -map.getView().getRotation();
141+
let center = map.getPixelFromCoordinate(map.getView().getCenter());
142+
let scaledP = scale(pixelP, center, pixelRatio);
143+
let rotatedP = rotate(scaledP, rotation, center);
144+
let result = [rotatedP[0] + offset[0], rotatedP[1] + offset[1]];
145+
let pixelGeometry = new ol.geom.Point(result);
146+
vectorContext.drawGeometry(pixelGeometry);
147+
return graphic;
148+
});
149+
}
150+
151+
152+
}

0 commit comments

Comments
 (0)