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
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
var layer1, style, logevt, lognoevt, map, lonlat, pixel, element;
function init() {
element = document.getElementById("map");
style = new OpenLayers.StyleMap({
'default': OpenLayers.Util.applyDefaults(
{label: "${l}", pointRadius: 30},
OpenLayers.Feature.Vector.style["default"]
),
'select': OpenLayers.Util.applyDefaults(
{pointRadius: 30},
OpenLayers.Feature.Vector.style.select
)
});
layer1 = new OpenLayers.Layer.Vector("Layer 1", {
styleMap: style
});
layer1.addFeatures([
new OpenLayers.Feature.Vector(OpenLayers.Geometry.fromWKT("POINT(0 0)"), {l:1}),
new OpenLayers.Feature.Vector(OpenLayers.Geometry.fromWKT("POINT(0 0)"), {l:1}),
new OpenLayers.Feature.Vector(OpenLayers.Geometry.fromWKT("POINT(0 0)"), {l:1}),
new OpenLayers.Feature.Vector(OpenLayers.Geometry.fromWKT("POINT(0 0)"), {l:1})
]);
map = new OpenLayers.Map({
div: "map",
allOverlays: true,
layers: [layer1],
zoom: 6,
center: [0, 0],
eventListeners: {
featureclick: logEvent,
nofeatureclick: logNoEvent
}
});
}
function logNoEvent(e) {
lognoevt.push(e);
}
function logEvent(e) {
logevt.push(e);
}
function trigger(type, pxl) {
var map_position = OpenLayers.Util.pagePosition(element);
map.events.triggerEvent(type, {
xy: pxl,
clientX: pxl.x + map_position[0],
clientY: pxl.y + map_position[1],
which: 1 // which == 1 means left-click
});
}
// TESTS
function test_onClick(t) {
t.plan(2);
logevt = [];
lognoevt = [];
lonlat = new OpenLayers.LonLat(0,0);
pixel = map.getPixelFromLonLat(lonlat);
trigger('mousedown', pixel);
trigger('mouseup', pixel);
t.eq(logevt.length, 4, "4 features hit");
trigger('mousedown', {x: 40, y: 40});
trigger('mouseup', {x: 40, y: 40});
t.eq(lognoevt.length, 1, "nofeatureclick fired for click outside features.");
}
// END TESTS
</script>
</head>
<body onload="init()">
<div id="map" style="width: 300px; height: 150px; border: 1px solid black"></div>
</body>
</html>
|