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_order) {
45 layer = options.layers_order[x];
46 console.log("setting up layer: " + layer + " with argument : " + options.layers[layer]);
47 if (layer.indexOf("Google") >= 0) {
48 if (options.google_apikey && options.google_apikey != 'null') {
49 var gtype = G_NORMAL_MAP;
51 if (options.layers[layer] == "Satellite") {
52 gtype = G_SATELLITE_MAP;
53 } else if (options.layers[layer] == "Hybrid") {
54 gtype = G_HYBRID_MAP // the normal map overlaying the satellite photographs
55 } else if (options.layers[layer] == "Physical") {
56 gtype = G_PHYSICAL_MAP // terrain information
58 // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html
59 googleLayer = new OpenLayers.Layer.Google(
60 "Google (" + options.layers[layer] + ")",
62 'sphericalMercator': true,
63 'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
64 projection: new OpenLayers.Projection("EPSG:3857")}
66 map.addLayer(googleLayer);
68 console.log("no API key defined for Google layer, skipping");
71 if (options.layers[layer] != 1) {
72 l = options.layers[layer];
73 fqdn = l.split("/")[2].split(".");
74 text = fqdn[fqdn.length-2] + "." + fqdn[fqdn.length-1];
75 map.addLayer(new OpenLayers.Layer.OSM(layer + " (" + text + ")", l));
77 map.addLayer(new OpenLayers.Layer.OSM(layer + " (Mapnik)"));
82 if (options.format == 'CSV') {
83 pois = new OpenLayers.Layer.Text( "CSV",
84 { location: options.csvurl,
85 projection: new OpenLayers.Projection("EPSG:4326")
87 } else if (options.format == 'GeoJSON') {
88 pois = new OpenLayers.Layer.Vector("GeoJSON", {
89 protocol: new OpenLayers.Protocol.HTTP({
91 format: new OpenLayers.Format.GeoJSON()
93 strategies: [new OpenLayers.Strategy.Fixed()],
94 projection: new OpenLayers.Projection("EPSG:4326")
97 pois = new OpenLayers.Layer.Vector("KML", {
98 protocol: new OpenLayers.Protocol.HTTP({
100 format: new OpenLayers.Format.KML({
102 extractAttributes: true
105 strategies: [new OpenLayers.Strategy.Fixed()],
106 projection: new OpenLayers.Projection("EPSG:4326")
110 select = new OpenLayers.Control.SelectFeature(pois);
111 map.addControl(select);
115 "featureselected": function (event) {
116 var feature = event.feature;
117 var content = '<h2><a href="' + feature.attributes.href + '">' +feature.attributes.name + "</a></h2>" + feature.attributes.description;
118 popup = new OpenLayers.Popup.FramedCloud("chicken",
119 feature.geometry.getBounds().getCenterLonLat(),
120 new OpenLayers.Size(100,100),
122 null, true, function () {select.unselectAll()});
123 feature.popup = popup;
126 "featureunselected": function (event) {
127 var feature = event.feature;
129 map.removePopup(feature.popup);
130 feature.popup.destroy();
131 delete feature.popup;
136 if (options.editable) {
137 vlayer = new OpenLayers.Layer.Vector( "Editable" );
138 map.addControl(new OpenLayers.Control.EditingToolbar(vlayer));
139 map.addLayer(vlayer);
142 if (options.fullscreen) {
143 map.addControl(new OpenLayers.Control.PanZoomBar());
144 map.addControl(new OpenLayers.Control.LayerSwitcher({'ascending':false}));
145 map.addControl(new OpenLayers.Control.MousePosition());
146 map.addControl(new OpenLayers.Control.KeyboardDefaults());
148 map.addControl(new OpenLayers.Control.ZoomPanel());
151 //Set start centrepoint and zoom
152 if (!options.lat || !options.lon) {
153 options.lat = urlParams['lat'];
154 options.lon = urlParams['lon'];
157 options.zoom = urlParams['zoom'];
159 if (options.lat && options.lon) {
160 var lat = options.lat;
161 var lon = options.lon;
162 var zoom= options.zoom || 10;
163 center = new OpenLayers.LonLat( lon, lat ).transform(
164 new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
165 map.getProjectionObject() // to Spherical Mercator Projection
167 map.setCenter (center, zoom);
169 pois.events.register("loadend", this, function () { map.zoomToExtent(pois.getDataExtent()); });