-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenlayer
299 lines (252 loc) · 8.94 KB
/
openlayer
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!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: 600px;
width: 100%;
}
.ol-popup{
position:absolute;
background-color:white;
padding:15px;
border-radius:10px;
border:1px solid #cccccc;
bottom:12px;
left: -50px;
min-width: 280px;
}
.ol-popup:after, .ol-popup:before {
top: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.ol-popup:after {
border-top-color: white;
border-width: 10px;
left: 48px;
margin-left: -10px;
}
.ol-popup:before {
border-top-color: #cccccc;
border-width: 11px;
left: 48px;
margin-left: -11px;
}
.ol-popup-closer {
text-decoration: none;
position: absolute;
top: 2px;
right: 8px;
}
.ol-popup-closer:after {
content: "✖";
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</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>
<div id="overlay">
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
<script type="text/javascript">
// styling the popup element
var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
// make an overlay to show popup on the map
var overlay = new ol.Overlay({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
/**
* Add a click handler to hide the popup.
* @return {boolean} Don't follow the href.
*/
closer.onclick = function() {
overlay.setPosition(undefined);
closer.blur();
return false;
};
var baseMapLayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var map = new ol.Map({
target: 'map',
layers: [ baseMapLayer],
view: new ol.View({
center: ol.proj.fromLonLat([-9.567156,30.412447]),
zoom: 9
})
});
var marker = new ol.Feature({
geometry: new ol.geom.Point(
ol.proj.fromLonLat([-9.567156,30.412447])
),
});
var marker2 = new ol.Feature({
geometry: new ol.geom.Point(
ol.proj.fromLonLat([-9.482459,30.379512])
),
});
var marker3 = new ol.Feature({
geometry: new ol.geom.Point(
ol.proj.fromLonLat([-9.382459,30.479512])
),
});
marker.setStyle(new ol.style.Style({
image: new ol.style.Icon(({
crossorigin:"anonymous",
src:'marker.png'
}))
}));
marker2.setStyle(new ol.style.Style({
image: new ol.style.Icon(({
crossorigin:"anonymous",
src:'marker.png'
}))
}));
marker3.setStyle(new ol.style.Style({
image: new ol.style.Icon(({
crossorigin:"anonymous",
src:'marker.png'
}))
}));
var vectorSource = new ol.source.Vector({
features: [marker,marker2,marker3]
});
var markerVectorLayer = new ol.layer.Vector({
source: vectorSource,
});
map.addLayer(markerVectorLayer);
console.log(marker3);
// register an event handler for the click event
map.on('click', function(event) {
// extract the spatial coordinate of the click event in map projection units
var coord = event.coordinate;
// transform it to decimal degrees
var degrees = ol.proj.transform(coord, 'EPSG:3857', 'EPSG:4326');
// format a human readable version
var hdms = ol.coordinate.toStringHDMS(degrees);
// update the overlay element's content
//var content = overlay.getElement();
content.innerHTML = '<p>Name : Station 1</p><table><tr><th>Date</th><th>T</th><th>Hr</th><th>Rg</th><th>P</th><th>Eto</th></tr><tr><td>A</td><td>M</td><td>G</td><td>d</td><td>c</td><td>w</td></tr></table>';
// position the element (using the coordinate in the map's projection)
overlay.setPosition(coord);
// and add it to the map
map.addOverlay(overlay);
});
</script>
</body>
</html>
//second virtion
<html>
<head>
<title>OpenLayers Marker Popups</title>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<div id="mapdiv"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/2.11/lib/OpenLayers.js"></script>
<script>
map = new OpenLayers.Map("mapdiv");
map.addLayer(new OpenLayers.Layer.OSM());
epsg4326 = new OpenLayers.Projection("EPSG:4326"); //WGS 1984 projection
projectTo = map.getProjectionObject(); //The map projection (Spherical Mercator)
var lonLat = new OpenLayers.LonLat(-9.567156,30.412447 ).transform(epsg4326, projectTo);
var zoom=11;
map.setCenter (lonLat, zoom);
// date variable show in the table
var date = new Date().toJSON().slice(0,10).replace(/-/g,'/');
var vectorLayer = new OpenLayers.Layer.Vector("Overlay");
// Define markers as "features" of the vector layer:
var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point( -9.567156,30.412447 ).transform(epsg4326, projectTo),
{description:'<p>Name : Station 1</p><table><tr><th>Date</th><th>T</th><th>Hr</th><th>Rg</th><th>P</th><th>Eto</th></tr><tr><td>'+date+'</td><td>12.00c</td><td>40.00%</td><td>20000.00</td><td>00.00</td><td>5.00</td></tr></table>'} ,
{externalGraphic: 'http://netanimations.net/colorchangearrowdown.gif', graphicHeight: 25, graphicWidth: 21, graphicXOffset:-12, graphicYOffset:-25 }
);
vectorLayer.addFeatures(feature);
var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point( -9.467156,30.312447 ).transform(epsg4326, projectTo),
{description:'<p>Name : Station 2</p><table><tr><th>Date</th><th>T</th><th>Hr</th><th>Rg</th><th>P</th><th>Eto</th></tr><tr><td>'+date+'</td><td>12.00c</td><td>40.00%</td><td>20000.00</td><td>00.00</td><td>5.00</td></tr></table>'} ,
{externalGraphic: 'http://netanimations.net/colorchangearrowdown.gif', graphicHeight: 25, graphicWidth: 21, graphicXOffset:-12, graphicYOffset:-25 }
);
vectorLayer.addFeatures(feature);
var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point( -9.482459,30.379512 ).transform(epsg4326, projectTo),
{description:'<p>Name : Station 3</p><table><tr><th>Date</th><th>T</th><th>Hr</th><th>Rg</th><th>P</th><th>Eto</th></tr><tr><td>'+date+'</td><td>12.00c</td><td>40.00%</td><td>20000.00</td><td>00.00</td><td>5.00</td></tr></table>'} ,
{externalGraphic: 'http://netanimations.net/colorchangearrowdown.gif', graphicHeight: 25, graphicWidth: 21, graphicXOffset:-12, graphicYOffset:-25 }
);
vectorLayer.addFeatures(feature);
var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(-9.382459,30.479512 ).transform(epsg4326, projectTo),
{description:'<p>Name : Station 4</p><table><tr><th>Date</th><th>T</th><th>Hr</th><th>Rg</th><th>P</th><th>Eto</th></tr><tr><td>'+date+'</td><td>12.00c</td><td>40.00%</td><td>20000.00</td><td>00.00</td><td>5.00</td></tr></table>'} ,
{externalGraphic: 'http://netanimations.net/colorchangearrowdown.gif', graphicHeight: 25, graphicWidth: 21, graphicXOffset:-12, graphicYOffset:-25 }
);
vectorLayer.addFeatures(feature);
map.addLayer(vectorLayer);
//Add a selector control to the vectorLayer with popup functions
var controls = {
selector: new OpenLayers.Control.SelectFeature(vectorLayer, { onSelect: createPopup, onUnselect: destroyPopup })
};
function createPopup(feature) {
feature.popup = new OpenLayers.Popup.FramedCloud("pop",
feature.geometry.getBounds().getCenterLonLat(),
null,
'<div class="markerContent">'+feature.attributes.description+'</div>',
null,
true,
function() { controls['selector'].unselectAll(); }
);
//feature.popup.closeOnMove = true;
map.addPopup(feature.popup);
}
function destroyPopup(feature) {
feature.popup.destroy();
feature.popup = null;
}
map.addControl(controls['selector']);
controls['selector'].activate();
</script>
</body>
</html>