]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
Redesign notes permalink
[rails.git] / app / assets / javascripts / map.js.erb
1 // Leaflet extensions
2 L.extend(L.LatLngBounds.prototype, {
3   getSouthLat: function () {
4     return this._southWest.lat;
5   },
6
7   getWestLng: function () {
8     return this._southWest.lng;
9   },
10
11   getNorthLat: function () {
12     return this._northEast.lat;
13   },
14
15   getEastLng: function () {
16     return this._northEast.lng;
17   },
18
19   toBBOX: function () {
20     var decimal = 6;
21     var mult = Math.pow(10, decimal);
22     var xmin = Math.round(this.getWestLng() * mult) / mult;
23     var ymin = Math.round(this.getSouthLat() * mult) / mult;
24     var xmax = Math.round(this.getEastLng() * mult) / mult;
25     var ymax = Math.round(this.getNorthLat() * mult) / mult;
26     return xmin + "," + ymin + "," + xmax + "," + ymax;
27   },
28
29   getSize: function () {
30     return (this._northEast.lat - this._southWest.lat) *
31            (this._northEast.lng - this._southWest.lng);
32   },
33
34   wrap: function () {
35     return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
36   }
37 });
38
39 L.extend(L.Bounds.prototype, {
40   getWidth: function () {
41    return this.max.x - this.min.x;
42   },
43
44   getHeight: function () {
45    return this.max.y - this.min.y;
46   }
47 });
48
49 L.Icon.Default.imagePath = <%= "#{asset_prefix}/images".to_json %>;
50
51 var map;
52 var objectLayer;
53 var objectLoader;
54
55 var layers = [
56   {
57     klass: L.OSM.Mapnik,
58     attribution: "",
59     keyid: "mapnik",
60     layerCode: "M",
61     name: I18n.t("javascripts.map.base.standard")
62   },
63   {
64     klass: L.OSM.CycleMap,
65     attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
66     keyid: "cyclemap",
67     layerCode: "C",
68     name: I18n.t("javascripts.map.base.cycle_map")
69   },
70   {
71     klass: L.OSM.TransportMap,
72     attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
73     keyid: "transportmap",
74     layerCode: "T",
75     name: I18n.t("javascripts.map.base.transport_map")
76   },
77   {
78     klass: L.OSM.MapQuestOpen,
79     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'>",
80     keyid: "mapquest",
81     layerCode: "Q",
82     name: I18n.t("javascripts.map.base.mapquest")
83   }
84 ];
85
86 function createMap(divName, options) {
87   options = $.extend({zoomControl: true, panZoomControl: true, layerControl: true}, options);
88
89   map = L.map(divName, $.extend({}, options, {panControl: false, zoomsliderControl: false, maxZoom: 18}));
90
91   if (map.attributionControl) {
92     map.attributionControl.setPrefix('');
93   }
94
95   if (options.panZoomControl) {
96     new L.Control.Pan().addTo(map);
97     new L.Control.Zoomslider({stepHeight: 7}).addTo(map);
98   }
99
100   var layersControl = L.control.layers();
101
102   if (options.layerControl) {
103     layersControl.addTo(map);
104     map.layersControl = layersControl;
105   }
106
107   for (var i = 0; i < layers.length; i++) {
108     layers[i].layer = new (layers[i].klass)(layers[i]);
109     layersControl.addBaseLayer(layers[i].layer, layers[i].name);
110   }
111
112   layers[0].layer.addTo(map);
113
114   $("#" + divName).on("resized", function () {
115     map.invalidateSize();
116   });
117
118   return map;
119 }
120
121 function getUserIcon(url) {
122   return L.icon({
123     iconUrl: url || <%= asset_path('marker-red.png').to_json %>,
124     iconSize: [25, 41],
125     iconAnchor: [12, 41],
126     popupAnchor: [1, -34],
127     shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
128     shadowSize: [41, 41]
129   });
130 }
131
132 function addObjectToMap(object, zoom, callback) {
133   if (objectLoader) {
134     objectLoader.abort();
135   }
136
137   if (objectLayer) {
138     map.removeLayer(objectLayer);
139   }
140
141   objectLoader = $.ajax({
142     url: OSM.apiUrl(object),
143     dataType: "xml",
144     success: function (xml) {
145       objectLayer = new L.OSM.DataLayer(xml, {
146         style: {
147           strokeColor: "blue",
148           strokeWidth: 3,
149           strokeOpacity: 0.5,
150           fillOpacity: 0.2,
151           fillColor: "lightblue",
152           pointRadius: 6
153         }
154       });
155
156       var bounds = objectLayer.getBounds();
157
158       if (zoom) {
159         map.fitBounds(bounds);
160       }
161
162       if (callback) {
163         callback(bounds);
164       }
165
166       objectLayer.addTo(map);
167     }
168   });
169 }
170
171 function addBoxToMap(bounds) {
172   var box = L.rectangle(bounds, {
173     weight: 2,
174     color: '#e90',
175     fillOpacity: 0
176   });
177
178   box.addTo(map);
179
180   return box;
181 }
182
183 function getMapBaseLayer() {
184   for (var i = 0; i < layers.length; i++) {
185     if (map.hasLayer(layers[i].layer)) {
186       return layers[i];
187     }
188   }
189 }
190
191 function getMapLayers() {
192   for (var i = 0; i < layers.length; i++) {
193     if (map.hasLayer(layers[i].layer)) {
194       return layers[i].layerCode;
195     }
196   }
197
198   return "";
199 }
200
201 function setMapLayers(layerConfig) {
202   var foundLayer = false;
203   for (var i = 0; i < layers.length; i++) {
204     if (layerConfig.indexOf(layers[i].layerCode) >= 0) {
205       map.addLayer(layers[i].layer);
206       foundLayer = true;
207     } else {
208       map.removeLayer(layers[i].layer);
209     }
210   }
211   if (!foundLayer) {
212     map.addLayer(layers[0].layer);
213   }
214 }