1 var epsg4326 = new OpenLayers.Projection("EPSG:4326");
7 function createMap(divName, options) {
8 options = options || {};
10 map = new OpenLayers.Map(divName, {
11 controls: options.controls || [
12 new OpenLayers.Control.ArgParser(),
13 new OpenLayers.Control.Attribution(),
14 new OpenLayers.Control.LayerSwitcher(),
15 new OpenLayers.Control.Navigation(),
16 new OpenLayers.Control.PanZoom(),
17 new OpenLayers.Control.PanZoomBar(),
18 new OpenLayers.Control.ScaleLine({geodesic: true})
21 maxResolution: 156543.0339,
23 displayProjection: new OpenLayers.Projection("EPSG:4326"),
24 theme: "<%= asset_path 'theme/default/style.css' %>"
27 var mapnik = new OpenLayers.Layer.OSM.Mapnik(i18n("javascripts.map.base.standard"), {
30 displayOutsideMaxExtent: true,
36 var cyclemap = new OpenLayers.Layer.OSM.CycleMap(i18n("javascripts.map.base.cycle_map"), {
37 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
39 displayOutsideMaxExtent: true,
43 map.addLayer(cyclemap);
45 var transportmap = new OpenLayers.Layer.OSM.TransportMap(i18n("javascripts.map.base.transport_map"), {
46 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
47 keyid: "transportmap",
48 displayOutsideMaxExtent: true,
52 map.addLayer(transportmap);
54 var mapquest = new OpenLayers.Layer.OSM(i18n("javascripts.map.base.mapquest"), [
55 "http://otile1.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
56 "http://otile2.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
57 "http://otile3.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
58 "http://otile4.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png"
60 attribution: "Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>",
62 displayOutsideMaxExtent: true,
67 map.addLayer(mapquest);
69 markers = new OpenLayers.Layer.Markers("Markers", {
70 displayInLayerSwitcher: false,
72 maxExtent: new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508),
73 maxResolution: 156543,
75 projection: "EPSG:900913"
77 map.addLayer(markers);
82 function getArrowIcon() {
83 var size = new OpenLayers.Size(25, 22);
84 var offset = new OpenLayers.Pixel(-30, -27);
85 var icon = new OpenLayers.Icon("<%= asset_path 'arrow.png' %>", size, offset);
90 function addMarkerToMap(position, icon, description) {
91 var marker = new OpenLayers.Marker(position.clone().transform(epsg4326, map.getProjectionObject()), icon);
93 markers.addMarker(marker);
96 marker.events.register("mouseover", marker, function() { openMapPopup(marker, description) });
102 function addObjectToMap(url, zoom, callback) {
103 var layer = new OpenLayers.Layer.GML("Objects", url, {
104 format: OpenLayers.Format.OSM,
110 fillColor: "lightblue",
113 projection: new OpenLayers.Projection("EPSG:4326"),
114 displayInLayerSwitcher: false
117 layer.events.register("loadend", layer, function() {
120 if (this.features.length) {
121 extent = this.features[0].geometry.getBounds();
123 for (var i = 1; i < this.features.length; i++) {
124 extent.extend(this.features[i].geometry.getBounds());
129 this.map.zoomToExtent(extent);
131 this.map.zoomToMaxExtent();
146 function addBoxToMap(boxbounds, id, outline) {
148 // Be aware that IE requires Vector layers be initialised on page load, and not under deferred script conditions
149 vectors = new OpenLayers.Layer.Vector("Boxes", {
150 displayInLayerSwitcher: false
152 map.addLayer(vectors);
156 vertices = boxbounds.toGeometry().getVertices();
157 vertices.push(new OpenLayers.Geometry.Point(vertices[0].x, vertices[0].y));
158 geometry = new OpenLayers.Geometry.LineString(vertices).transform(epsg4326, map.getProjectionObject());
160 geometry = boxbounds.toGeometry().transform(epsg4326, map.getProjectionObject());
162 var box = new OpenLayers.Feature.Vector(geometry, {}, {
164 strokeColor: '#ee9900',
169 vectors.addFeatures(box);
174 function openMapPopup(marker, description) {
177 popup = new OpenLayers.Popup.FramedCloud("popup", marker.lonlat, null,
178 description, marker.icon, true);
179 popup.setBackgroundColor("#E3FFC5");
185 function closeMapPopup() {
187 map.removePopup(popup);
192 function removeMarkerFromMap(marker){
193 markers.removeMarker(marker);
196 function removeBoxFromMap(box){
197 vectors.removeFeature(box);
200 function getMapCenter() {
201 return map.getCenter().clone().transform(map.getProjectionObject(), epsg4326);
204 function setMapCenter(center, zoom) {
205 zoom = parseInt(zoom);
206 var numzoom = map.getNumZoomLevels();
207 if (zoom >= numzoom) zoom = numzoom - 1;
208 map.setCenter(center.clone().transform(epsg4326, map.getProjectionObject()), zoom);
211 function setMapExtent(extent) {
212 map.zoomToExtent(extent.clone().transform(epsg4326, map.getProjectionObject()));
215 function getMapExtent() {
216 return map.getExtent().clone().transform(map.getProjectionObject(), epsg4326);
219 function getMapZoom() {
220 return map.getZoom();
223 function getEventPosition(event) {
224 return map.getLonLatFromViewPortPx(event.xy).clone().transform(map.getProjectionObject(), epsg4326);
227 function getMapLayers() {
228 var layerConfig = "";
230 for (var i = 0; i < map.layers.length; i++) {
231 if (map.layers[i].layerCode && map.layers[i].getVisibility()) {
232 layerConfig += map.layers[i].layerCode;
239 function setMapLayers(layerConfig) {
240 if (layerConfig.charAt(0) == "B" || layerConfig.charAt(0) == "0") {
243 for (var layers = map.getLayersBy("isBaseLayer", true), i = 0; i < layers.length; i++) {
244 var c = layerConfig.charAt(l++);
247 map.setBaseLayer(layers[i]);
249 map.layers[i].setVisibility(false);
253 for (var i = 0; i < map.layers.length; i++) {
254 if (map.layers[i].layerCode) {
255 if (layerConfig.indexOf(map.layers[i].layerCode) >= 0) {
256 if (map.layers[i].isBaseLayer) {
257 map.setBaseLayer(map.layers[i]);
259 map.layers[i].setVisibility(true);
261 } else if (!map.layers[i].isBaseLayer) {
262 map.layers[i].setVisibility(false);
269 function scaleToZoom(scale) {
270 return Math.log(360.0/(scale * 512.0)) / Math.log(2.0);