GISCafe Guest Andreas Hocevar
Andreas works on the development and architecture of Boundless SDK, OpenLayers 3. Previously he’s worked on the development of OpenLayers 2, GeoExt, and GeoExplorer. Andreas is an accomplished open source developer; he is an active steering committee member for both OpenLayers and GeoExt . He has … More » Census Mapping Made Easy with OpenLayers 3July 8th, 2014 by Andreas Hocevar
Three months ago, Paul Ramsey created a tutorial that builds an example census mapping application using OpenGeo Suite. Since then, we’ve released OpenGeo Suite 4.0, which includes early access to OpenLayers 3, and we’ve updated the tutorial to use OpenLayers 3 instead of GeoExt. A Different Approach With GeoExt, the easiest way to read in the list of available census variables (topics) was to pre-process them and load them as JSON. Rather than spending much time on user interface work, we’ve added Bootstrap to leverage the JQuery dependency and save some lines of JavaScript code. With JQuery, a few lines of code are enough to create drop-down options directly from the lines of the original metadata text file:
$.get(“../data/DataDict.txt”, function(response) { // We start at line 3 – line 1 is column names, line 2 is not a variable $(response.split(‘\n’).splice(2)).each(function(index, line) { $(‘#topics’).append($(‘<option>’) .val(line.substr(0, 10).trim()) .html(line.substr(10, 105).trim())); }); }); The HTML side of this drop-down is straightforward, with Bootstrap’s form-controlCSS class providing a nice appearance: <select id=”topics” class=”form-control”></select> Simpler API olMap = new ol.Map({ target: ‘map’, renderer: ol.RendererHint.CANVAS, layers: [osmLayer, wmsLayer], view: new ol.View2D({ center: [-10764594.0, 4523072.0], zoom: 5 }) }); Compare this to the old OpenLayers 2 and GeoExt code: olMap = new OpenLayers.Map({ projection: “EPSG:900913”, units: “m”, layers: [wmsLayer, osmLayer] }); olMap.setCenter([-10764594.0, 4523072.0], 5); var viewPort = new Ext.Viewport({ layout: “fit”, items: [{ xtype: “gx_mappanel”, ref: “mappanel”, map: olMap }] }); Flexible Integration var popup = new ol.Overlay({ element: $(‘#popup’) }); olMap.addOverlay(popup); When information about the selected point is available, the overlay is anchored to the map at the coordinates of the click event. The markup for the popup comes from a Bootstrap Popover: function (info) { popup.setPosition(event.getCoordinate()); $(‘#popup’) .popover({content: info.join(”)}) .popover(‘show’); } Try OpenLayers 3 Visit the official website or try our census mapping tutorial to learn more about using OpenLayers 3. Tags: Andreas Hocevar, boundless, boundlessgeo, census map, extjs, geoext, geospatial, jquery, json, mapping, mobile, opengeo, openlayers Category: Boundless |