1 // taken from http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
5 a = /\\+/g, // Regex for replacing addition symbol with a space
6 r = /([^&=]+)=?([^&]*)/g,
7 d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
8 q = window.location.search.substring(1);
11 urlParams[d(e[1])] = d(e[2]);
14 function mapsetup(divname, options) {
15 div = document.getElementById(divname);
16 if (options.fullscreen) {
17 permalink = 'permalink';
20 div.style.position = 'absolute';
21 div.style.width = '100%';
22 div.style.height = '100%';
25 div.style.height = options.height;
26 div.style.width = options.width;
27 div.style.float = options.float;
28 permalink = {base: options.href, title: "View larger map"};
30 map = new OpenLayers.Map(divname, {
32 new OpenLayers.Control.Navigation(),
33 new OpenLayers.Control.ScaleLine(),
34 new OpenLayers.Control.Permalink(permalink)
36 displayProjection: new OpenLayers.Projection("EPSG:4326"),
37 maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
38 projection: "EPSG:900913",
40 maxResolution: 156543.0339,
44 for (x in options.layers) {
45 layer = options.layers[x];
46 console.log("setting up layer: " + layer);
47 if (layer.indexOf("Google") >= 0) {
48 if (options.google_apikey && options.google_apikey != 'null') {
49 var gtype = G_NORMAL_MAP;
50 if (layer.indexOf("Satellite") >= 0) {
51 gtype = G_SATELLITE_MAP;
52 } else if (layer.indexOf("Hybrid") >= 0) {
53 gtype = G_HYBRID_MAP // the normal map overlaying the satellite photographs
54 } else if (layer.indexOf("Physical") >= 0) {
55 gtype = G_PHYSICAL_MAP // terrain information
57 // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html
58 googleLayer = new OpenLayers.Layer.Google(
61 'sphericalMercator': true,
62 'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
63 projection: new OpenLayers.Projection("EPSG:3857")}
65 map.addLayer(googleLayer);
67 console.log("no API key defined for Google layer, skipping");
69 } else if (layer == 'OSM') { // OSM default layer
70 map.addLayer(new OpenLayers.Layer.OSM("OSM (Mapnik)"));
71 } else { // assumed to be a URL
72 text = layer.match(/([^.\/]*\.[^.\/]*(\/[^\$]*)?)\/.*$/i) // take the first two parts of the FQDN and everything before the first $
73 map.addLayer(new OpenLayers.Layer.OSM("OSM (" + text[1] + ")", layer));
77 if (options.format == 'CSV') {
78 pois = new OpenLayers.Layer.Text( "CSV",
79 { location: options.csvurl,
80 projection: new OpenLayers.Projection("EPSG:4326")
82 } else if (options.format == 'GeoJSON') {
83 pois = new OpenLayers.Layer.Vector("GeoJSON", {
84 protocol: new OpenLayers.Protocol.HTTP({
86 format: new OpenLayers.Format.GeoJSON()
88 strategies: [new OpenLayers.Strategy.Fixed()],
89 projection: new OpenLayers.Projection("EPSG:4326")
92 pois = new OpenLayers.Layer.Vector("KML", {
93 protocol: new OpenLayers.Protocol.HTTP({
95 format: new OpenLayers.Format.KML({
97 extractAttributes: true
100 strategies: [new OpenLayers.Strategy.Fixed()],
101 projection: new OpenLayers.Projection("EPSG:4326")
105 select = new OpenLayers.Control.SelectFeature(pois);
106 map.addControl(select);
110 "featureselected": function (event) {
111 var feature = event.feature;
112 var content = '<h2><a href="' + feature.attributes.href + '">' +feature.attributes.name + "</a></h2>" + feature.attributes.description;
113 popup = new OpenLayers.Popup.FramedCloud("chicken",
114 feature.geometry.getBounds().getCenterLonLat(),
115 new OpenLayers.Size(100,100),
117 null, true, function () {select.unselectAll()});
118 feature.popup = popup;
121 "featureunselected": function (event) {
122 var feature = event.feature;
124 map.removePopup(feature.popup);
125 feature.popup.destroy();
126 delete feature.popup;
131 if (options.editable) {
132 vlayer = new OpenLayers.Layer.Vector( "Editable" );
133 map.addControl(new OpenLayers.Control.EditingToolbar(vlayer));
134 map.addLayer(vlayer);
137 if (options.fullscreen) {
138 map.addControl(new OpenLayers.Control.PanZoomBar());
139 map.addControl(new OpenLayers.Control.LayerSwitcher());
140 map.addControl(new OpenLayers.Control.MousePosition());
141 map.addControl(new OpenLayers.Control.KeyboardDefaults());
143 map.addControl(new OpenLayers.Control.ZoomPanel());
146 //Set start centrepoint and zoom
147 if (!options.lat || !options.lon) {
148 options.lat = urlParams['lat'];
149 options.lon = urlParams['lon'];
152 options.zoom = urlParams['zoom'];
154 if (options.lat && options.lon) {
155 var lat = options.lat;
156 var lon = options.lon;
157 var zoom= options.zoom || 10;
158 center = new OpenLayers.LonLat( lon, lat ).transform(
159 new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
160 map.getProjectionObject() // to Spherical Mercator Projection
162 map.setCenter (center, zoom);
164 pois.events.register("loadend", this, function () { map.zoomToExtent(pois.getDataExtent()); });