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",
122 projection: new OpenLayers.Projection("EPSG:4326"),
123 displayInLayerSwitcher: false
126 layer.events.register("loadend", layer, function() {
129 if (this.features.length) {
130 extent = this.features[0].geometry.getBounds();
132 for (var i = 1; i < this.features.length; i++) {
133 extent.extend(this.features[i].geometry.getBounds());
138 this.map.zoomToExtent(extent);
140 this.map.zoomToMaxExtent();
155 function addBoxToMap(boxbounds) {
157 // Be aware that IE requires Vector layers be initialised on page load, and not under deferred script conditions
158 vectors = new OpenLayers.Layer.Vector("Box Layer", {
159 displayInLayerSwitcher: false
161 map.addLayer(vectors);
163 var geometry = boxbounds.toGeometry().transform(epsg4326, map.getProjectionObject());
164 var box = new OpenLayers.Feature.Vector(geometry, {}, {
166 strokeColor: '#ee9900',
170 vectors.addFeatures(box);
175 function openMapPopup(marker, description) {
178 popup = new OpenLayers.Popup.AnchoredBubble("popup", marker.lonlat, null,
179 description, marker.icon, true);
180 popup.setBackgroundColor("#E3FFC5");
181 popup.autoSize = true;
187 function closeMapPopup() {
189 map.removePopup(popup);
194 function removeMarkerFromMap(marker){
195 markers.removeMarker(marker);
198 function removeBoxFromMap(box){
199 vectors.removeFeature(box);
202 function getMapCenter(center, zoom) {
203 return map.getCenter().clone().transform(map.getProjectionObject(), epsg4326);
206 function setMapCenter(center, zoom) {
207 zoom = parseInt(zoom);
208 var numzoom = map.getNumZoomLevels();
209 if (zoom >= numzoom) zoom = numzoom - 1;
210 map.setCenter(center.clone().transform(epsg4326, map.getProjectionObject()), zoom);
213 function setMapExtent(extent) {
214 map.zoomToExtent(extent.clone().transform(epsg4326, map.getProjectionObject()));
217 function getMapExtent() {
218 return map.getExtent().clone().transform(map.getProjectionObject(), epsg4326);
221 function getEventPosition(event) {
222 return map.getLonLatFromViewPortPx(event.xy).clone().transform(map.getProjectionObject(), epsg4326);
225 function getMapLayers() {
226 var layerConfig = "";
228 for (var layers = map.getLayersBy("isBaseLayer", true), i = 0; i < layers.length; i++) {
229 layerConfig += layers[i] == map.baseLayer ? "B" : "0";
232 for (var layers = map.getLayersBy("isBaseLayer", false), i = 0; i < layers.length; i++) {
233 layerConfig += layers[i].getVisibility() ? "T" : "F";
239 function setMapLayers(layerConfig) {
242 for (var layers = map.getLayersBy("isBaseLayer", true), i = 0; i < layers.length; i++) {
243 var c = layerConfig.charAt(l++);
246 map.setBaseLayer(layers[i]);
250 while (layerConfig.charAt(l) == "B" || layerConfig.charAt(l) == "0") {
254 for (var layers = map.getLayersBy("isBaseLayer", false), i = 0; i < layers.length; i++) {
255 var c = layerConfig.charAt(l++);
258 layers[i].setVisibility(true);
259 } else if(c == "F") {
260 layers[i].setVisibility(false);
265 function scaleToZoom(scale) {
266 return Math.log(360.0/(scale * 512.0)) / Math.log(2.0);