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.Zoom(),
17 new OpenLayers.Control.SimplePanZoom(),
18 new OpenLayers.Control.ScaleLine({geodesic: true})
21 displayProjection: new OpenLayers.Projection("EPSG:4326"),
22 theme: "<%= asset_path 'theme/openstreetmap/style.css' %>"
25 var mapnik = new OpenLayers.Layer.OSM.Mapnik(I18n.t("javascripts.map.base.standard"), {
28 displayOutsideMaxExtent: true,
34 var cyclemap = new OpenLayers.Layer.OSM.CycleMap(I18n.t("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.t("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.t("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 projection: "EPSG:900913"
72 map.addLayer(markers);
74 map.dataLayer = new OpenLayers.Layer(I18n.t('browse.start_rjs.data_layer_name'), {
76 displayInLayerSwitcher: false
78 map.addLayer(map.dataLayer);
80 $("#" + divName).on("resized", function () {
84 $("#" + divName).trigger("initialised");
89 function getArrowIcon() {
90 var size = new OpenLayers.Size(25, 22);
91 var offset = new OpenLayers.Pixel(-22, -20);
92 var icon = new OpenLayers.Icon("<%= asset_path 'arrow.png' %>", size, offset);
97 function addMarkerToMap(position, icon, description) {
98 var marker = new OpenLayers.Marker(proj(position), icon);
100 markers.addMarker(marker);
103 marker.events.register("mouseover", marker, function() {
104 openMapPopup(marker, description);
111 function addObjectToMap(object, zoom, callback) {
112 var layer = new OpenLayers.Layer.Vector("Objects", {
114 new OpenLayers.Strategy.Fixed()
116 protocol: new OpenLayers.Protocol.HTTP({
117 url: OSM.apiUrl(object),
118 format: new OpenLayers.Format.OSM()
125 fillColor: "lightblue",
128 projection: new OpenLayers.Projection("EPSG:4326"),
129 displayInLayerSwitcher: false
132 layer.events.register("loadend", layer, function() {
135 if (this.features.length) {
136 extent = this.features[0].geometry.getBounds();
138 for (var i = 1; i < this.features.length; i++) {
139 extent.extend(this.features[i].geometry.getBounds());
144 this.map.zoomToExtent(extent);
146 this.map.zoomToMaxExtent();
159 function addBoxToMap(boxbounds, id, outline) {
161 // Be aware that IE requires Vector layers be initialised on page load, and not under deferred script conditions
162 vectors = new OpenLayers.Layer.Vector("Boxes", {
163 displayInLayerSwitcher: false
165 map.addLayer(vectors);
169 vertices = boxbounds.toGeometry().getVertices();
170 vertices.push(new OpenLayers.Geometry.Point(vertices[0].x, vertices[0].y));
171 geometry = proj(new OpenLayers.Geometry.LineString(vertices));
173 geometry = proj(boxbounds.toGeometry());
175 var box = new OpenLayers.Feature.Vector(geometry, {}, {
177 strokeColor: '#ee9900',
182 vectors.addFeatures(box);
187 function openMapPopup(marker, description) {
190 popup = new OpenLayers.Popup.FramedCloud("popup", marker.lonlat, null,
191 description, marker.icon, true);
192 popup.setBackgroundColor("#E3FFC5");
198 function closeMapPopup() {
200 map.removePopup(popup);
204 function removeMarkerFromMap(marker){
205 markers.removeMarker(marker);
209 return x.clone().transform(epsg4326, map.getProjectionObject());
213 return x.clone().transform(map.getProjectionObject(), epsg4326);
216 function setMapCenter(center, zoom) {
217 zoom = parseInt(zoom, 10);
218 var numzoom = map.getNumZoomLevels();
219 if (zoom >= numzoom) zoom = numzoom - 1;
220 map.setCenter(proj(center), zoom);
223 function getEventPosition(event) {
224 return unproj(map.getLonLatFromViewPortPx(event.xy));
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);