-
Notifications
You must be signed in to change notification settings - Fork 47
/
ol_simple.js
70 lines (64 loc) · 2.42 KB
/
ol_simple.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
// all the interaction stuff is copied almost verbatim from
// http://www.openlayers.org/dev/examples/dynamic-text-layer.html
window.onload = function () {
map = new OpenLayers.Map('map', {sphericalMercator: true});
var osm = new OpenLayers.Layer.OSM({sphericalMercator: true});
var shpLayer = new OpenLayers.Layer.Vector({projection: new OpenLayers.Projection('EPSG:4326')});
map.addLayers([osm, shpLayer]);
map.setCenter(new OpenLayers.LonLat(0, 0), 1);
// Interaction; not needed for initial display.
selectControl = new OpenLayers.Control.SelectFeature(shpLayer);
map.addControl(selectControl);
selectControl.activate();
shpLayer.events.on({
'featureselected': onFeatureSelect,
'featureunselected': onFeatureUnselect
});
// load the shapefile
var theUrl = 'naturalearthdata/cultural/110m-admin-0-countries';
getOpenLayersFeatures(theUrl, function (fs) {
// reproject features
// this is ordinarily done by the format object, but since we're adding features manually we have to do it.
var fsLen = fs.length;
var inProj = new OpenLayers.Projection('EPSG:4326');
var outProj = new OpenLayers.Projection('EPSG:3857');
for (var i = 0; i < fsLen; i++) {
fs[i].geometry = fs[i].geometry.transform(inProj, outProj);
}
shpLayer.addFeatures(fs);
});
}
// Needed only for interaction, not for the display.
function onPopupClose(evt) {
// 'this' is the popup.
var feature = this.feature;
if (feature.layer) { // The feature is not destroyed
selectControl.unselect(feature);
} else { // After "moveend" or "refresh" events on POIs layer all
// features have been destroyed by the Strategy.BBOX
this.destroy();
}
}
function onFeatureSelect(evt) {
feature = evt.feature;
var table = '<table>';
for (var attr in feature.attributes.values) {
table += '<tr><td>' + attr + '</td><td>' + feature.attributes.values[attr] + '</td></tr>';
}
table += '</table>';
popup = new OpenLayers.Popup.FramedCloud("featurePopup",
feature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size(100,100), table, null, true, onPopupClose);
feature.popup = popup;
popup.feature = feature;
map.addPopup(popup, true);
}
function onFeatureUnselect(evt) {
feature = evt.feature;
if (feature.popup) {
popup.feature = null;
map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
}
}