-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.html
155 lines (140 loc) · 5.64 KB
/
popup.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
<!DOCTYPE HTML>
<html>
<head>
<title>OpenLayers Popup</title>
<style type="text/css">
html, body, #basicMap {
margin: 0;
width: 100%;
height: 100%;
}
</style>
<script src="js/OpenLayers.js"></script>
<script>
var circle_style = {
strokeColor: '#ff0000',
strokeOpacity: 0.5,
strokeWidth: 3,
fill: true
};
var markersLayer, marker, selectControl, layer, control;
var mapnik, fromProjection, toProjection, position;
function init(lat,lon,zoom) {
map = new OpenLayers.Map("basicMap", {
projection: new OpenLayers.Projection("EPSG:3857"),
// this sets wgs84/4326 as default for display coords
displayProjection: new OpenLayers.Projection("EPSG:4326")
});
mapnik = new OpenLayers.Layer.OSM();
fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
position = new OpenLayers.LonLat(lon,lat).transform( fromProjection, toProjection );
map.addLayer(mapnik);
map.addControl(new OpenLayers.Control.Permalink('permalink'));
map.addControl(new OpenLayers.Control.MousePosition());
// try a marker layer
// =================== FAILED ====================
markersLayer = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markersLayer);
marker = new OpenLayers.Marker(position);
marker.id = "1";
markersLayer.addMarker(marker);
// this code causes an event on a click on the marker, BUT
// openlayers reports #<Object> has no method 'getFeatureFromEvent'
// using either markersLayer.events.on(), or marker.events.on()
var select = new OpenLayers.Control.SelectFeature(markersLayer);
//markersLayer.events.on({
marker.events.on({
"featureselected": onFeatureSelect,
"featureunselected": onFeatureUnselect
});
map.addControl(select);
select.activate();
//marker.events.register("mousedown", marker, function() {
//alert('Clicked marker with id '+this.id);
// onFeatureSelect(this);
//});
// ============================================== */
/* ==================== FAILED =======================
// markersLayer.events.on({
//marker.events.on({
layer.events.on({
'featureselected': onFeatureSelect,
'featureunselected': onFeatureUnselect
});
selectControl = new OpenLayers.Control.SelectFeature(layer);
//selectControl = new OpenLayers.Control.SelectFeature(markersLayer);
//selectControl = new OpenLayers.Control.SelectFeature(marker);
===================================================== */
/* ==================== FAILED =======================
layer = new OpenLayers.Layer.Vector("Vector Layer");
control = new OpenLayers.Control.SelectFeature(layer);
map.addControl(control);
control.activate();
layer.events.register("featureselected", layer, selected);
var radius = 2000; // TODO: what SIZE should the circle be? probably should DEPEND on zoom???
// distance to vertex, in map units
var sides = 30; // docs say '20 approximates a circle'... so ...
var point = new OpenLayers.Geometry.Point(position.lon, position.lat);
var circle = OpenLayers.Geometry.Polygon.createRegularPolygon(
point,
radius,
sides,
0 );
var featurecircle = new OpenLayers.Feature.Vector(circle, null, circle_style);
//vectorLayer.addFeatures([featurecircle]);
layer.addFeatures([featurecircle]);
===================================================== */
map.setCenter(position, zoom );
}
function selected (evt) {
alert(evt.feature.id + " selected on " + this.name);
}
function onPopupClose(evt) {
// 'this' is the popup.
selectControl.unselect(this.feature);
}
function onFeatureSelect(evt) {
var feature = evt.feature;
console.log("Feature selected...");
//feature.geometry.getBounds().getCenterLonLat(),
var popup = new OpenLayers.Popup.FramedCloud("featurePopup",
evt.lonlat,
new OpenLayers.Size(100,100),
"<h2>title: "+evt.id+ "</h2>" +
"desc: "+evt.id,
null, true, onPopupClose);
feature.popup = popup;
popup.feature = feature;
map.addPopup(popup);
}
function onFeatureUnselect(evt) {
var feature = evt.feature;
if (feature.popup) {
popup.feature = null;
map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
}
}
</script>
</head>
<body>
<div id="basicMap"></div>
<script type="text/javascript" defer="defer">
function gup2( name, def ) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return def;
return results[1];
}
var lat = gup2('lat',52.52);
var lon = gup2('lon',13.41);
var zoom = gup2('zoom',15);
init(lat,lon,zoom);
</script>
</body>
</html>