]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
ba331f313b4da6167ccaad4fbfa0fba934e895b6
[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(null, {
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       objectLayer.interestingNode = function (node, ways, relations) {
157         if (object.type === "node") {
158           return true;
159         } else if (object.type === "relation") {
160           for (var i = 0; i < relations.length; i++)
161             if (relations[i].members.indexOf(node) != -1)
162               return true;
163         } else {
164           return false;
165         }
166       };
167
168       objectLayer.addData(xml);
169
170       var bounds = objectLayer.getBounds();
171
172       if (zoom) {
173         map.fitBounds(bounds);
174       }
175
176       if (callback) {
177         callback(bounds);
178       }
179
180       objectLayer.addTo(map);
181     }
182   });
183 }
184
185 function addBoxToMap(bounds) {
186   var box = L.rectangle(bounds, {
187     weight: 2,
188     color: '#e90',
189     fillOpacity: 0
190   });
191
192   box.addTo(map);
193
194   return box;
195 }
196
197 function getMapBaseLayer() {
198   for (var i = 0; i < layers.length; i++) {
199     if (map.hasLayer(layers[i].layer)) {
200       return layers[i];
201     }
202   }
203 }
204
205 function getMapLayers() {
206   var layerConfig = "";
207   for (var i = 0; i < layers.length; i++) {
208     if (map.hasLayer(layers[i].layer)) {
209       layerConfig += layers[i].layerCode;
210     }
211   }
212   return layerConfig;
213 }
214
215 function setMapLayers(layerConfig) {
216   var foundLayer = false;
217   for (var i = 0; i < layers.length; i++) {
218     if (layerConfig.indexOf(layers[i].layerCode) >= 0) {
219       map.addLayer(layers[i].layer);
220       foundLayer = true;
221     } else {
222       map.removeLayer(layers[i].layer);
223     }
224   }
225   if (!foundLayer) {
226     map.addLayer(layers[0].layer);
227   }
228 }