summaryrefslogtreecommitdiff
path: root/misc/openlayers/examples/select-feature-multilayer.html
blob: 6b8f3f4abbccc38cb30cf9fb3e5287cc0d10c596 (plain)
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
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <title>SelectFeature Control on multiple vector layers</title> 
    <link rel="stylesheet" href="../theme/default/style.css" type="text/css">
    <link rel="stylesheet" href="style.css" type="text/css">
    <style type="text/css">
        #controlToggle li {
            list-style: none;
        }
    </style>
    <script src="../lib/OpenLayers.js"></script>
    <script type="text/javascript">
        var map, selectControl;
        OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '2';
        function init(){
            map = new OpenLayers.Map('map');
            var wmsLayer = new OpenLayers.Layer.WMS(
                "OpenLayers WMS", 
                "http://vmap0.tiles.osgeo.org/wms/vmap0",
                {layers: 'basic'}
            ); 

            // allow testing of specific renderers via "?renderer=Canvas", etc
            var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
            renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;

            var vectors1 = new OpenLayers.Layer.Vector("Vector Layer 1", {
                renderers: renderer,
                styleMap: new OpenLayers.StyleMap({
                    "default": new OpenLayers.Style(OpenLayers.Util.applyDefaults({
                        externalGraphic: "../img/marker-green.png",
                        graphicOpacity: 1,
                        rotation: -45,
                        pointRadius: 10
                    }, OpenLayers.Feature.Vector.style["default"])),
                    "select": new OpenLayers.Style({
                        externalGraphic: "../img/marker-blue.png"
                    })
                })
            });
            var vectors2 = new OpenLayers.Layer.Vector("Vector Layer 2", {
                renderers: renderer,
                styleMap: new OpenLayers.StyleMap({
                    "default": new OpenLayers.Style(OpenLayers.Util.applyDefaults({
                        fillColor: "red",
                        strokeColor: "gray",
                        graphicName: "square",
                        rotation: 45,
                        pointRadius: 15
                    }, OpenLayers.Feature.Vector.style["default"])),
                    "select": new OpenLayers.Style(OpenLayers.Util.applyDefaults({
                        graphicName: "square",
                        rotation: 45,
                        pointRadius: 15
                    }, OpenLayers.Feature.Vector.style["select"]))
                })
            });
            map.addLayers([wmsLayer, vectors1, vectors2]);
            map.addControl(new OpenLayers.Control.LayerSwitcher());
            
            selectControl = new OpenLayers.Control.SelectFeature(
                [vectors1, vectors2],
                {
                    clickout: true, toggle: false,
                    multiple: false, hover: false,
                    toggleKey: "ctrlKey", // ctrl key removes from selection
                    multipleKey: "shiftKey" // shift key adds to selection
                }
            );
            
            map.addControl(selectControl);
            selectControl.activate();
            map.setCenter(new OpenLayers.LonLat(0, 0), 3);
            vectors1.addFeatures(createFeatures());
            vectors2.addFeatures(createFeatures());
            
            vectors1.events.on({
                "featureselected": function(e) {
                    showStatus("selected feature "+e.feature.id+" on Vector Layer 1");
                },
                "featureunselected": function(e) {
                    showStatus("unselected feature "+e.feature.id+" on Vector Layer 1");
                }
            });
            vectors2.events.on({
                "featureselected": function(e) {
                    showStatus("selected feature "+e.feature.id+" on Vector Layer 2");
                },
                "featureunselected": function(e) {
                    showStatus("unselected feature "+e.feature.id+" on Vector Layer 2");
                }
            });
        }
        
        function createFeatures() {
            var extent = map.getExtent();
            var features = [];
            for(var i=0; i<10; ++i) {
                features.push(new OpenLayers.Feature.Vector(
                    new OpenLayers.Geometry.Point(extent.left + (extent.right - extent.left) * Math.random(),
                    extent.bottom + (extent.top - extent.bottom) * Math.random()
                )));
            }
            return features;
        }
        
        function showStatus(text) {
            document.getElementById("status").innerHTML = text;            
        }

    </script>
  </head>
  <body onload="init()">
    <h1 id="title">OpenLayers Select Feature on Multiple Layers Example</h1>
    <div id="tags">
        vector, feature, selecting, selection, advanced, light
    </div>     
    <p id="shortdesc">
      Select a feature on click with the Control.SelectFeature on multiple 
      vector layers.
    </p>  
    <div id="map" class="smallmap"></div>
    <div id="status"></div>
  </body>
</html>