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 SimpleLayerSwitcher(),
15 new OpenLayers.Control.Navigation(),
16 new OpenLayers.Control.PanZoom(),
17 new OpenLayers.Control.PanZoomBar(),
18 new OpenLayers.Control.ScaleLine({geodesic: true})
21 displayProjection: new OpenLayers.Projection("EPSG:4326"),
22 theme: "<%= asset_path 'theme/default/style.css' %>"
25 var mapnik = new OpenLayers.Layer.OSM.Mapnik(i18n("javascripts.map.base.standard"), {
28 displayOutsideMaxExtent: true,
34 var cyclemap = new OpenLayers.Layer.OSM.CycleMap(i18n("javascripts.map.base.cycle_map"), {
35 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
37 displayOutsideMaxExtent: true,
41 map.addLayer(cyclemap);
43 var transportmap = new OpenLayers.Layer.OSM.TransportMap(i18n("javascripts.map.base.transport_map"), {
44 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
45 keyid: "transportmap",
46 displayOutsideMaxExtent: true,
50 map.addLayer(transportmap);
52 var mapquest = new OpenLayers.Layer.OSM(i18n("javascripts.map.base.mapquest"), [
53 "http://otile1.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
54 "http://otile2.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
55 "http://otile3.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
56 "http://otile4.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png"
58 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'>",
60 displayOutsideMaxExtent: true,
65 map.addLayer(mapquest);
67 markers = new OpenLayers.Layer.Markers("Markers", {
68 displayInLayerSwitcher: false,
70 maxExtent: new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508),
71 projection: "EPSG:900913"
73 map.addLayer(markers);
78 function getArrowIcon() {
79 var size = new OpenLayers.Size(25, 22);
80 var offset = new OpenLayers.Pixel(-30, -27);
81 var icon = new OpenLayers.Icon("<%= asset_path 'arrow.png' %>", size, offset);
86 function addMarkerToMap(position, icon, description) {
87 var marker = new OpenLayers.Marker(proj(position), icon);
89 markers.addMarker(marker);
92 marker.events.register("mouseover", marker, function() {
93 openMapPopup(marker, description);
100 function addObjectToMap(url, zoom, callback) {
101 var layer = new OpenLayers.Layer.Vector("Objects", {
103 new OpenLayers.Strategy.Fixed()
105 protocol: new OpenLayers.Protocol.HTTP({
107 format: new OpenLayers.Format.OSM()
114 fillColor: "lightblue",
117 projection: new OpenLayers.Projection("EPSG:4326"),
118 displayInLayerSwitcher: false
121 layer.events.register("loadend", layer, function() {
124 if (this.features.length) {
125 extent = this.features[0].geometry.getBounds();
127 for (var i = 1; i < this.features.length; i++) {
128 extent.extend(this.features[i].geometry.getBounds());
133 this.map.zoomToExtent(extent);
135 this.map.zoomToMaxExtent();
148 function addBoxToMap(boxbounds, id, outline) {
150 // Be aware that IE requires Vector layers be initialised on page load, and not under deferred script conditions
151 vectors = new OpenLayers.Layer.Vector("Boxes", {
152 displayInLayerSwitcher: false
154 map.addLayer(vectors);
158 vertices = boxbounds.toGeometry().getVertices();
159 vertices.push(new OpenLayers.Geometry.Point(vertices[0].x, vertices[0].y));
160 geometry = proj(new OpenLayers.Geometry.LineString(vertices));
162 geometry = proj(boxbounds.toGeometry());
164 var box = new OpenLayers.Feature.Vector(geometry, {}, {
166 strokeColor: '#ee9900',
171 vectors.addFeatures(box);
176 function openMapPopup(marker, description) {
179 popup = new OpenLayers.Popup.FramedCloud("popup", marker.lonlat, null,
180 description, marker.icon, true);
181 popup.setBackgroundColor("#E3FFC5");
187 function closeMapPopup() {
189 map.removePopup(popup);
194 function removeMarkerFromMap(marker){
195 markers.removeMarker(marker);
198 function removeBoxFromMap(box){
199 vectors.removeFeature(box);
203 return x.clone().transform(epsg4326, map.getProjectionObject());
207 return x.clone().transform(map.getProjectionObject(), epsg4326);
210 function setMapCenter(center, zoom) {
211 zoom = parseInt(zoom, 10);
212 var numzoom = map.getNumZoomLevels();
213 if (zoom >= numzoom) zoom = numzoom - 1;
214 map.setCenter(proj(center), zoom);
217 function getEventPosition(event) {
218 return map.getLonLatFromViewPortPx(event.xy).clone().transform(map.getProjectionObject(), epsg4326);
221 function getMapLayers() {
222 var layerConfig = "";
224 for (var i = 0; i < map.layers.length; i++) {
225 if (map.layers[i].layerCode && map.layers[i].getVisibility()) {
226 layerConfig += map.layers[i].layerCode;
233 function setMapLayers(layerConfig) {
234 if (layerConfig.charAt(0) == "B" || layerConfig.charAt(0) == "0") {
237 for (var layers = map.getLayersBy("isBaseLayer", true), i = 0; i < layers.length; i++) {
238 var c = layerConfig.charAt(l++);
241 map.setBaseLayer(layers[i]);
243 map.layers[i].setVisibility(false);
247 for (var i = 0; i < map.layers.length; i++) {
248 if (map.layers[i].layerCode) {
249 if (layerConfig.indexOf(map.layers[i].layerCode) >= 0) {
250 if (map.layers[i].isBaseLayer) {
251 map.setBaseLayer(map.layers[i]);
253 map.layers[i].setVisibility(true);
255 } else if (!map.layers[i].isBaseLayer) {
256 map.layers[i].setVisibility(false);