-
Notifications
You must be signed in to change notification settings - Fork 459
/
Globe.js
178 lines (167 loc) · 4.93 KB
/
Globe.js
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
/* Copyright (c) 2016 Jean-Marc VIGLINO,
released under the CeCILL-B license (French BSD license)
(http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt).
*/
import ol_ext_inherits from '../util/ext'
import {unByKey as ol_Observable_unByKey} from 'ol/Observable'
import ol_control_Control from 'ol/control/Control'
import ol_Map from 'ol/Map'
import ol_Collection from 'ol/Collection'
import ol_View from 'ol/View'
import ol_layer_Vector from 'ol/layer/Vector'
import ol_style_Style from 'ol/style/Style'
import ol_style_Circle from 'ol/style/Circle'
import ol_style_Fill from 'ol/style/Fill'
import ol_style_Stroke from 'ol/style/Stroke'
import ol_source_Vector from 'ol/source/Vector'
/**
* OpenLayers 3 lobe Overview Control.
* The globe can rotate with map (follow.)
*
* @constructor
* @extends {ol_control_Control}
* @param {Object=} options Control options.
* @param {boolean} follow follow the map when center change, default false
* @param {top|bottom-left|right} align position as top-left, etc.
* @param {Array<ol.layer>} layers list of layers to display on the globe
* @param {ol.style.Style | Array.<ol.style.Style> | undefined} style style to draw the position on the map , default a marker
*/
class ol_control_Globe {
constructor(opt_options) {
var options = opt_options || {};
var self = this;
// API
var element;
if (options.target) {
element = document.createElement("div");
this.panel_ = options.target;
}
else {
element = document.createElement("div");
element.classList.add('ol-globe', 'ol-unselectable', 'ol-control');
if (/top/.test(options.align))
element.classList.add('ol-control-top');
if (/right/.test(options.align))
element.classList.add('ol-control-right');
this.panel_ = document.createElement("div");
this.panel_.classList.add("panel");
element.appendChild(this.panel_);
this.pointer_ = document.createElement("div");
this.pointer_.classList.add("ol-pointer");
element.appendChild(this.pointer_);
}
ol_control_Control.call(this, {
element: element,
target: options.target
});
// http://openlayers.org/en/latest/examples/sphere-mollweide.html ???
// Create a globe map
this.ovmap_ = new ol_Map({
controls: new ol_Collection(),
interactions: new ol_Collection(),
target: this.panel_,
view: new ol_View({
zoom: 0,
center: [0, 0]
}),
layers: options.layers
});
setTimeout(function () {
self.ovmap_.updateSize();
}, 0);
this.set('follow', options.follow || false);
// Cache extent
this.extentLayer = new ol_layer_Vector({
name: 'Cache extent',
source: new ol_source_Vector(),
style: options.style || [new ol_style_Style({
image: new ol_style_Circle({
fill: new ol_style_Fill({
color: 'rgba(255,0,0, 1)'
}),
stroke: new ol_style_Stroke({
width: 7,
color: 'rgba(255,0,0, 0.8)'
}),
radius: 5
})
})]
});
this.ovmap_.addLayer(this.extentLayer);
}
/**
* Set the map instance the control associated with.
* @param {ol.Map} map The map instance.
*/
setMap(map) {
if (this._listener)
ol_Observable_unByKey(this._listener);
this._listener = null;
ol_control_Control.prototype.setMap.call(this, map);
// Get change (new layer added or removed)
if (map) {
this._listener = map.getView().on('propertychange', this.setView.bind(this));
this.setView();
}
}
/** Set the globe center with the map center
*/
setView() {
if (this.getMap() && this.get('follow')) {
this.setCenter(this.getMap().getView().getCenter());
}
}
/** Get globe map
* @return {ol_Map}
*/
getGlobe() {
return this.ovmap_;
}
/** Show/hide the globe
*/
show(b) {
if (b !== false)
this.element.classList.remove("ol-collapsed");
else
this.element.classList.add("ol-collapsed");
this.ovmap_.updateSize();
}
/** Set position on the map
* @param {top|bottom-left|right} align
*/
setPosition(align) {
if (/top/.test(align))
this.element.classList.add("ol-control-top");
else
this.element.classList.remove("ol-control-top");
if (/right/.test(align))
this.element.classList.add("ol-control-right");
else
this.element.classList.remove("ol-control-right");
}
/** Set the globe center
* @param {_ol_coordinate_} center the point to center to
* @param {boolean} show show a pointer on the map, defaylt true
*/
setCenter(center, show) {
var self = this;
this.pointer_.classList.add("hidden");
if (center) {
var map = this.ovmap_;
var p = map.getPixelFromCoordinate(center);
if (p) {
if (show !== false) {
var h = this.element.clientHeight;
setTimeout(function () {
self.pointer_.style.top = String(Math.min(Math.max(p[1], 0), h)) + 'px';
self.pointer_.style.left = "50%";
self.pointer_.classList.remove("hidden");
}, 800);
}
map.getView().animate({ center: [center[0], 0] });
}
}
}
}
ol_ext_inherits(ol_control_Globe, ol_control_Control);
export default ol_control_Globe