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")
26 var mapnik = new OpenLayers.Layer.OSM.Mapnik(i18n("javascripts.map.base.mapnik"), {
28 displayOutsideMaxExtent: true,
34 var osmarender = new OpenLayers.Layer.OSM.Osmarender(i18n("javascripts.map.base.osmarender"), {
36 displayOutsideMaxExtent: true,
40 map.addLayer(osmarender);
42 var cyclemap = new OpenLayers.Layer.OSM.CycleMap(i18n("javascripts.map.base.cycle_map"), {
44 displayOutsideMaxExtent: true,
48 map.addLayer(cyclemap);
50 var transportmap = new OpenLayers.Layer.OSM.TransportMap(i18n("javascripts.map.base.transport_map"), {
51 keyid: "transportmap",
52 displayOutsideMaxExtent: true,
56 map.addLayer(transportmap);
58 var mapquest = new OpenLayers.Layer.OSM(i18n("javascripts.map.base.mapquest"), [
59 "http://otile1.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
60 "http://otile2.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
61 "http://otile3.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
62 "http://otile4.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png"
65 displayOutsideMaxExtent: true,
70 map.addLayer(mapquest);
72 var numZoomLevels = Math.max(mapnik.numZoomLevels, osmarender.numZoomLevels);
74 markers = new OpenLayers.Layer.Markers("Markers", {
75 displayInLayerSwitcher: false,
76 numZoomLevels: numZoomLevels,
77 maxExtent: new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508),
78 maxResolution: 156543,
80 projection: "EPSG:900913"
82 map.addLayer(markers);
87 function getArrowIcon() {
88 var size = new OpenLayers.Size(25, 22);
89 var offset = new OpenLayers.Pixel(-30, -27);
90 var icon = new OpenLayers.Icon("<%= asset_path 'arrow.png' %>", size, offset);
95 function addMarkerToMap(position, icon, description) {
96 var marker = new OpenLayers.Marker(position.clone().transform(epsg4326, map.getProjectionObject()), icon);
98 markers.addMarker(marker);
101 marker.events.register("mouseover", marker, function() { openMapPopup(marker, description) });
107 function addObjectToMap(url, zoom, callback) {
108 var layer = new OpenLayers.Layer.GML("Objects", url, {
109 format: OpenLayers.Format.OSM,
115 fillColor: "lightblue",
118 projection: new OpenLayers.Projection("EPSG:4326"),
119 displayInLayerSwitcher: false
122 layer.events.register("loadend", layer, function() {
125 if (this.features.length) {
126 extent = this.features[0].geometry.getBounds();
128 for (var i = 1; i < this.features.length; i++) {
129 extent.extend(this.features[i].geometry.getBounds());
134 this.map.zoomToExtent(extent);
136 this.map.zoomToMaxExtent();
151 function addBoxToMap(boxbounds, id, outline) {
153 // Be aware that IE requires Vector layers be initialised on page load, and not under deferred script conditions
154 vectors = new OpenLayers.Layer.Vector("Boxes", {
155 displayInLayerSwitcher: false
157 map.addLayer(vectors);
161 vertices = boxbounds.toGeometry().getVertices();
162 vertices.push(new OpenLayers.Geometry.Point(vertices[0].x, vertices[0].y));
163 geometry = new OpenLayers.Geometry.LineString(vertices).transform(epsg4326, map.getProjectionObject());
165 geometry = boxbounds.toGeometry().transform(epsg4326, map.getProjectionObject());
167 var box = new OpenLayers.Feature.Vector(geometry, {}, {
169 strokeColor: '#ee9900',
174 vectors.addFeatures(box);
179 function openMapPopup(marker, description) {
182 popup = new OpenLayers.Popup.FramedCloud("popup", marker.lonlat, null,
183 description, marker.icon, true);
184 popup.setBackgroundColor("#E3FFC5");
190 function closeMapPopup() {
192 map.removePopup(popup);
197 function removeMarkerFromMap(marker){
198 markers.removeMarker(marker);
201 function removeBoxFromMap(box){
202 vectors.removeFeature(box);
205 function getMapCenter() {
206 return map.getCenter().clone().transform(map.getProjectionObject(), epsg4326);
209 function setMapCenter(center, zoom) {
210 zoom = parseInt(zoom);
211 var numzoom = map.getNumZoomLevels();
212 if (zoom >= numzoom) zoom = numzoom - 1;
213 map.setCenter(center.clone().transform(epsg4326, map.getProjectionObject()), zoom);
216 function setMapExtent(extent) {
217 map.zoomToExtent(extent.clone().transform(epsg4326, map.getProjectionObject()));
220 function getMapExtent() {
221 return map.getExtent().clone().transform(map.getProjectionObject(), epsg4326);
224 function getMapZoom() {
225 return map.getZoom();
228 function getEventPosition(event) {
229 return map.getLonLatFromViewPortPx(event.xy).clone().transform(map.getProjectionObject(), epsg4326);
232 function getMapLayers() {
233 var layerConfig = "";
235 for (var i = 0; i < map.layers.length; i++) {
236 if (map.layers[i].layerCode && map.layers[i].getVisibility()) {
237 layerConfig += map.layers[i].layerCode;
244 function setMapLayers(layerConfig) {
245 if (layerConfig.charAt(0) == "B" || layerConfig.charAt(0) == "0") {
248 for (var layers = map.getLayersBy("isBaseLayer", true), i = 0; i < layers.length; i++) {
249 var c = layerConfig.charAt(l++);
252 map.setBaseLayer(layers[i]);
254 map.layers[i].setVisibility(false);
258 for (var i = 0; i < map.layers.length; i++) {
259 if (map.layers[i].layerCode) {
260 if (layerConfig.indexOf(map.layers[i].layerCode) >= 0) {
261 if (map.layers[i].isBaseLayer) {
262 map.setBaseLayer(map.layers[i]);
264 map.layers[i].setVisibility(true);
267 map.layers[i].setVisibility(false);
274 function scaleToZoom(scale) {
275 return Math.log(360.0/(scale * 512.0)) / Math.log(2.0);