1 page.replace_html :sidebar_title, 'Browse'
2 page.replace_html :sidebar_content, :partial => 'start'
5 var gml, sf, objList, currentFeature;
6 OpenLayers.Feature.Vector.style['default'].strokeWidth = 3;
7 OpenLayers.Feature.Vector.style['default'].cursor = "pointer";
10 openSidebar({ onclose: stopBrowse });
11 var vectors = new OpenLayers.Layer.Vector();
13 box = new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.RegularPolygon, {
19 callbacks: { done: endDrag }
25 function stopBrowse() {
35 currentFeature.destroy();
36 currentFeature = null;
40 function startDrag() {
41 $("drag_box").innerHTML='Drag a box on the map to select an area';
45 $("drag_box").onclick = startDrag;
48 var bounds = map.getExtent();
53 $("use_map").onclick = useMap;
55 function endDrag(bbox) {
56 var bounds = bbox.getBounds();
60 $("drag_box").innerHTML = "Manually select a different area";
63 function getData(bounds) {
64 $("status").innerHTML = "Loading...";
66 bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
67 var url = "/api/0.5/map?bbox="+bounds.toBBOX();
70 var def = OpenLayers.Feature.Vector.style['default'];
71 var style = new OpenLayers.Style();
72 style.addRules([new OpenLayers.Rule(
74 {"Polygon": {'fillColor': '#ff0000', 'strokeColor': '#ff0000'},
75 "Line": {'fillColor': '#ffff00', 'strokeColor': '#000000', strokeOpacity: '0.4'},
76 "Point": {'fillColor': '#00ff00', 'strokeColor': '#00ff00'}}
79 gml = new OpenLayers.Layer.GML("Data",url,
80 {format: OpenLayers.Format.OSM, formatOptions: {checkTags: true},
81 styleMap: new OpenLayers.StyleMap({'default': style, 'select': {'strokeColor': '#0000ff'}})
84 gml.events.register("loadend", gml, dataLoaded );
87 sf = new OpenLayers.Control.SelectFeature(gml, {'onSelect': onFeatureSelect});
95 function dataLoaded() {
96 $("status").innerHTML = "Loaded " + this.features.length + " features. (<a href='"+ this.url+"'>API</a>)";
98 objList = document.createElement("ul");
99 for (var i = 0; i < this.features.length; i++) {
100 var feature = this.features[i];
104 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
107 var nice_name = type.substr(0,1).toUpperCase() + type.substr(1,type.length);
108 var li = document.createElement("li");
109 li.appendChild(document.createTextNode(nice_name + " "));
111 // Link, for viewing in the tab
112 var link = document.createElement("a");
113 link.href = "/browse/" + type + "/" + feature.osm_id;
114 var name = feature.attributes.name || feature.osm_id;
115 link.appendChild(document.createTextNode(name));
116 link.feature = feature;
117 link.onclick = OpenLayers.Function.bind(viewFeatureLink, link);
118 li.appendChild(link);
120 objList.appendChild(li);
122 $("object").innerHTML = "";
123 $("object").appendChild(objList);
126 function viewFeatureLink() {
127 var layer = this.feature.layer;
128 for (var i = 0; i < layer.selectedFeatures.length; i++) {
129 var f = layer.selectedFeatures[i];
130 layer.drawFeature(f, layer.styleMap.createSymbolizer(f, "default"));
132 onFeatureSelect(this.feature);
133 map.setCenter(this.feature.geometry.getBounds().getCenterLonLat());
137 function loadObjList() {
138 $("object").innerHTML="";
139 $("object").appendChild(objList);
143 function onFeatureSelect(feature) {
144 // Unselect previously selected feature
145 if (currentFeature) {
146 currentFeature.layer.drawFeature(
147 currentFeature, currentFeature.layer.styleMap.createSymbolizer(currentFeature, "default")
151 // Redraw in selected style
152 feature.layer.drawFeature(
153 feature, feature.layer.styleMap.createSymbolizer(feature, "select")
156 // If the current object is the list, don't innerHTML="", since that could clar it.
157 if ($("object").firstChild == objList) {
158 $("object").removeChild(objList);
160 $("object").innerHTML = "";
163 // Create a link back to the object list
164 var div = document.createElement("div");
165 var link = document.createElement("a");
167 link.onclick = loadObjList;
168 link.appendChild(document.createTextNode("Back to Object List"));
169 div.appendChild(link)
170 $("object").appendChild(div);
172 // Now the list of attributes
173 var ul = document.createElement("ul");
175 if (feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
178 var li = document.createElement("li");
179 var link = document.createElement("a");
180 link.href = "/browse/"+type+"/"+feature.osm_id;
181 link.appendChild(document.createTextNode(feature.osm_id));
182 li.appendChild(link);
184 for (var key in feature.attributes) {
185 var li = document.createElement("li");
186 var b = document.createElement("b");
187 b.appendChild(document.createTextNode(key));
189 li.appendChild(document.createTextNode(": " + feature.attributes[key]));
192 $("object").appendChild(ul);
194 // Stash the currently drawn feature
195 currentFeature = feature;
198 function setBounds(bounds) {
199 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
200 var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
202 bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
204 $("minlon").innerHTML = Math.round(bounds.left * decimals) / decimals;
205 $("minlat").innerHTML = Math.round(bounds.bottom * decimals) / decimals;
206 $("maxlon").innerHTML = Math.round(bounds.right * decimals) / decimals;
207 $("maxlat").innerHTML = Math.round(bounds.top * decimals) / decimals;