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.mapnik"), {
30 displayOutsideMaxExtent: true,
36 var osmarender = new OpenLayers.Layer.OSM.Osmarender(i18n("javascripts.map.base.osmarender"), {
39 displayOutsideMaxExtent: true,
43 map.addLayer(osmarender);
45 var cyclemap = new OpenLayers.Layer.OSM.CycleMap(i18n("javascripts.map.base.cycle_map"), {
46 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
48 displayOutsideMaxExtent: true,
52 map.addLayer(cyclemap);
54 var transportmap = new OpenLayers.Layer.OSM.TransportMap(i18n("javascripts.map.base.transport_map"), {
55 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
56 keyid: "transportmap",
57 displayOutsideMaxExtent: true,
61 map.addLayer(transportmap);
63 var mapquest = new OpenLayers.Layer.OSM(i18n("javascripts.map.base.mapquest"), [
64 "http://otile1.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
65 "http://otile2.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
66 "http://otile3.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
67 "http://otile4.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png"
69 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'>",
71 displayOutsideMaxExtent: true,
76 map.addLayer(mapquest);
78 var numZoomLevels = Math.max(mapnik.numZoomLevels, osmarender.numZoomLevels);
80 markers = new OpenLayers.Layer.Markers("Markers", {
81 displayInLayerSwitcher: false,
82 numZoomLevels: numZoomLevels,
83 maxExtent: new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508),
84 maxResolution: 156543,
86 projection: "EPSG:900913"
88 map.addLayer(markers);
93 function getArrowIcon() {
94 var size = new OpenLayers.Size(25, 22);
95 var offset = new OpenLayers.Pixel(-30, -27);
96 var icon = new OpenLayers.Icon("<%= asset_path 'arrow.png' %>", size, offset);
101 function addMarkerToMap(position, icon, description) {
102 var marker = new OpenLayers.Marker(position.clone().transform(epsg4326, map.getProjectionObject()), icon);
104 markers.addMarker(marker);
107 marker.events.register("mouseover", marker, function() { openMapPopup(marker, description) });
113 function addObjectToMap(url, zoom, callback) {
114 var layer = new OpenLayers.Layer.GML("Objects", url, {
115 format: OpenLayers.Format.OSM,
121 fillColor: "lightblue",
124 projection: new OpenLayers.Projection("EPSG:4326"),
125 displayInLayerSwitcher: false
128 layer.events.register("loadend", layer, function() {
131 if (this.features.length) {
132 extent = this.features[0].geometry.getBounds();
134 for (var i = 1; i < this.features.length; i++) {
135 extent.extend(this.features[i].geometry.getBounds());
140 this.map.zoomToExtent(extent);
142 this.map.zoomToMaxExtent();
157 function addBoxToMap(boxbounds, id, outline) {
159 // Be aware that IE requires Vector layers be initialised on page load, and not under deferred script conditions
160 vectors = new OpenLayers.Layer.Vector("Boxes", {
161 displayInLayerSwitcher: false
163 map.addLayer(vectors);
167 vertices = boxbounds.toGeometry().getVertices();
168 vertices.push(new OpenLayers.Geometry.Point(vertices[0].x, vertices[0].y));
169 geometry = new OpenLayers.Geometry.LineString(vertices).transform(epsg4326, map.getProjectionObject());
171 geometry = boxbounds.toGeometry().transform(epsg4326, map.getProjectionObject());
173 var box = new OpenLayers.Feature.Vector(geometry, {}, {
175 strokeColor: '#ee9900',
180 vectors.addFeatures(box);
185 function openMapPopup(marker, description) {
188 popup = new OpenLayers.Popup.FramedCloud("popup", marker.lonlat, null,
189 description, marker.icon, true);
190 popup.setBackgroundColor("#E3FFC5");
196 function closeMapPopup() {
198 map.removePopup(popup);
203 function removeMarkerFromMap(marker){
204 markers.removeMarker(marker);
207 function removeBoxFromMap(box){
208 vectors.removeFeature(box);
211 function getMapCenter() {
212 return map.getCenter().clone().transform(map.getProjectionObject(), epsg4326);
215 function setMapCenter(center, zoom) {
216 zoom = parseInt(zoom);
217 var numzoom = map.getNumZoomLevels();
218 if (zoom >= numzoom) zoom = numzoom - 1;
219 map.setCenter(center.clone().transform(epsg4326, map.getProjectionObject()), zoom);
222 function setMapExtent(extent) {
223 map.zoomToExtent(extent.clone().transform(epsg4326, map.getProjectionObject()));
226 function getMapExtent() {
227 return map.getExtent().clone().transform(map.getProjectionObject(), epsg4326);
230 function getMapZoom() {
231 return map.getZoom();
234 function getEventPosition(event) {
235 return map.getLonLatFromViewPortPx(event.xy).clone().transform(map.getProjectionObject(), epsg4326);
238 function getMapLayers() {
239 var layerConfig = "";
241 for (var i = 0; i < map.layers.length; i++) {
242 if (map.layers[i].layerCode && map.layers[i].getVisibility()) {
243 layerConfig += map.layers[i].layerCode;
250 function setMapLayers(layerConfig) {
251 if (layerConfig.charAt(0) == "B" || layerConfig.charAt(0) == "0") {
254 for (var layers = map.getLayersBy("isBaseLayer", true), i = 0; i < layers.length; i++) {
255 var c = layerConfig.charAt(l++);
258 map.setBaseLayer(layers[i]);
260 map.layers[i].setVisibility(false);
264 for (var i = 0; i < map.layers.length; i++) {
265 if (map.layers[i].layerCode) {
266 if (layerConfig.indexOf(map.layers[i].layerCode) >= 0) {
267 if (map.layers[i].isBaseLayer) {
268 map.setBaseLayer(map.layers[i]);
270 map.layers[i].setVisibility(true);
272 } else if (!map.layers[i].isBaseLayer) {
273 map.layers[i].setVisibility(false);
280 function scaleToZoom(scale) {
281 return Math.log(360.0/(scale * 512.0)) / Math.log(2.0);