1 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
8 'openstreetmap.org': '2f59745a6b525b4ebdb100891d5b6711',
9 'www.openstreetmap.org': 'fd093e52f0965d46bb1c6c6281022199',
10 'openstreetmap.com': '4c60e7f5f31c576a9bb8da71c8d61152',
11 'www.openstreetmap.com': '142f25a0770a51a9a400b3513834a199',
12 'openstreetmap.net': '687c58fd1d715596bfc94abe653d8ac0',
13 'www.openstreetmap.net': '0bd1654141c85d30b9c2ccdb5302f2e4'
16 OpenLayers._getScriptLocation = function () {
17 // Should really have this file as an erb, so that this can return
18 // the real rails root
19 return "/openlayers/";
22 function createMap(divName, options) {
23 options = options || {};
25 map = new OpenLayers.Map(divName, {
26 controls: options.controls || [
27 new OpenLayers.Control.ArgParser(),
28 new OpenLayers.Control.Attribution(),
29 new OpenLayers.Control.LayerSwitcher(),
30 new OpenLayers.Control.Navigation(),
31 new OpenLayers.Control.PanZoomBar(),
32 new OpenLayers.Control.ScaleLine()
35 maxResolution: 156543.0339,
37 displayProjection: new OpenLayers.Projection("EPSG:4326")
40 var mapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik", {
41 displayOutsideMaxExtent: true,
46 var osmarender = new OpenLayers.Layer.OSM.Osmarender("Osmarender", {
47 displayOutsideMaxExtent: true,
50 map.addLayer(osmarender);
52 var cyclemap = new OpenLayers.Layer.OSM.CycleMap("Cycle Map", {
53 displayOutsideMaxExtent: true,
56 map.addLayer(cyclemap);
58 var nonamekey = nonamekeys[document.domain];
59 var noname = new OpenLayers.Layer.OSM("NoName", [
60 "http://a.tile.cloudmade.com/" + nonamekey + "/3/256/${z}/${x}/${y}.png",
61 "http://b.tile.cloudmade.com/" + nonamekey + "/3/256/${z}/${x}/${y}.png",
62 "http://c.tile.cloudmade.com/" + nonamekey + "/3/256/${z}/${x}/${y}.png"
64 displayOutsideMaxExtent: true,
70 var maplint = new OpenLayers.Layer.OSM.Maplint("Maplint", {
71 displayOutsideMaxExtent: true,
74 map.addLayer(maplint);
76 var numZoomLevels = Math.max(mapnik.numZoomLevels, osmarender.numZoomLevels);
78 markers = new OpenLayers.Layer.Markers("Markers", {
79 displayInLayerSwitcher: false,
80 numZoomLevels: numZoomLevels,
81 maxExtent: new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508),
82 maxResolution: 156543,
84 projection: "EPSG:900913"
86 map.addLayer(markers);
91 function getArrowIcon() {
92 var size = new OpenLayers.Size(25, 22);
93 var offset = new OpenLayers.Pixel(-30, -27);
94 var icon = new OpenLayers.Icon("/images/arrow.png", size, offset);
99 function addMarkerToMap(position, icon, description) {
100 var marker = new OpenLayers.Marker(position.clone().transform(epsg4326, map.getProjectionObject()), icon);
102 markers.addMarker(marker);
105 marker.events.register("click", marker, function() { openMapPopup(marker, description) });
111 function addObjectToMap(url, zoom, callback) {
112 var layer = new OpenLayers.Layer.GML("Objects", url, {
113 format: OpenLayers.Format.OSM,
119 fillColor: "lightblue"
121 projection: new OpenLayers.Projection("EPSG:4326"),
122 displayInLayerSwitcher: false
125 layer.events.register("loadend", layer, function() {
128 if (this.features.length) {
129 extent = this.features[0].geometry.getBounds();
131 for (var i = 1; i < this.features.length; i++) {
132 extent.extend(this.features[i].geometry.getBounds());
137 this.map.zoomToExtent(extent);
139 this.map.zoomToMaxExtent();
154 function addBoxToMap(boxbounds) {
156 // Be aware that IE requires Vector layers be initialised on page load, and not under deferred script conditions
157 vectors = new OpenLayers.Layer.Vector("Box Layer", {
158 displayInLayerSwitcher: false
160 map.addLayer(vectors);
162 var geometry = boxbounds.toGeometry().transform(epsg4326, map.getProjectionObject());
163 var box = new OpenLayers.Feature.Vector(geometry, {}, {
165 strokeColor: '#ee9900',
169 vectors.addFeatures(box);
174 function openMapPopup(marker, description) {
177 popup = new OpenLayers.Popup.AnchoredBubble("popup", marker.lonlat, null,
178 description, marker.icon, true);
179 popup.setBackgroundColor("#E3FFC5");
180 popup.autoSize = true;
186 function closeMapPopup() {
188 map.removePopup(popup);
193 function removeMarkerFromMap(marker){
194 markers.removeMarker(marker);
197 function removeBoxFromMap(box){
198 vectors.removeFeature(box);
201 function getMapCenter(center, zoom) {
202 return map.getCenter().clone().transform(map.getProjectionObject(), epsg4326);
205 function setMapCenter(center, zoom) {
206 zoom = parseInt(zoom);
207 var numzoom = map.getNumZoomLevels();
208 if (zoom >= numzoom) zoom = numzoom - 1;
209 map.setCenter(center.clone().transform(epsg4326, map.getProjectionObject()), zoom);
212 function setMapExtent(extent) {
213 map.zoomToExtent(extent.clone().transform(epsg4326, map.getProjectionObject()));
216 function getMapExtent() {
217 return map.getExtent().clone().transform(map.getProjectionObject(), epsg4326);
220 function getEventPosition(event) {
221 return map.getLonLatFromViewPortPx(event.xy).clone().transform(map.getProjectionObject(), epsg4326);
224 function getMapLayers() {
225 var layerConfig = "";
227 for (var layers = map.getLayersBy("isBaseLayer", true), i = 0; i < layers.length; i++) {
228 layerConfig += layers[i] == map.baseLayer ? "B" : "0";
231 for (var layers = map.getLayersBy("isBaseLayer", false), i = 0; i < layers.length; i++) {
232 layerConfig += layers[i].getVisibility() ? "T" : "F";
238 function setMapLayers(layerConfig) {
241 for (var layers = map.getLayersBy("isBaseLayer", true), i = 0; i < layers.length; i++) {
242 var c = layerConfig.charAt(l++);
245 map.setBaseLayer(layers[i]);
249 while (layerConfig.charAt(l) == "B" || layerConfig.charAt(l) == "0") {
253 for (var layers = map.getLayersBy("isBaseLayer", false), i = 0; i < layers.length; i++) {
254 var c = layerConfig.charAt(l++);
257 layers[i].setVisibility(true);
258 } else if(c == "F") {
259 layers[i].setVisibility(false);
264 function scaleToZoom(scale) {
265 return Math.log(360.0/(scale * 512.0)) / Math.log(2.0);