]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
Show OSM objects for Nominatim search results as map overlays
[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
54 var layers = [
55   {
56     klass: L.OSM.Mapnik,
57     attribution: "",
58     keyid: "mapnik",
59     layerCode: "M",
60     name: I18n.t("javascripts.map.base.standard")
61   },
62   {
63     klass: L.OSM.CycleMap,
64     attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
65     keyid: "cyclemap",
66     layerCode: "C",
67     name: I18n.t("javascripts.map.base.cycle_map")
68   },
69   {
70     klass: L.OSM.TransportMap,
71     attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
72     keyid: "transportmap",
73     layerCode: "T",
74     name: I18n.t("javascripts.map.base.transport_map")
75   },
76   {
77     klass: L.OSM.MapQuestOpen,
78     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'>",
79     keyid: "mapquest",
80     layerCode: "Q",
81     name: I18n.t("javascripts.map.base.mapquest")
82   }
83 ];
84
85 function createMap(divName, options) {
86   options = $.extend({zoomControl: true, panZoomControl: true, layerControl: true}, options);
87
88   map = L.map(divName, $.extend({}, options, {panControl: false, zoomsliderControl: false, maxZoom: 18}));
89
90   if (map.attributionControl) {
91     map.attributionControl.setPrefix('');
92   }
93
94   if (options.panZoomControl) {
95     new L.Control.Pan().addTo(map);
96     new L.Control.Zoomslider({stepHeight: 7}).addTo(map);
97   }
98
99   var layersControl = L.control.layers();
100
101   if (options.layerControl) {
102     layersControl.addTo(map);
103     map.layersControl = layersControl;
104   }
105
106   for (var i = 0; i < layers.length; i++) {
107     layers[i].layer = new (layers[i].klass)(layers[i]);
108     layersControl.addBaseLayer(layers[i].layer, layers[i].name);
109   }
110
111   layers[0].layer.addTo(map);
112
113   $("#" + divName).on("resized", function () {
114     map.invalidateSize();
115   });
116
117   return map;
118 }
119
120 function getUserIcon(url) {
121   return L.icon({
122     iconUrl: url || <%= asset_path('marker-red.png').to_json %>,
123     iconSize: [25, 41],
124     iconAnchor: [12, 41],
125     popupAnchor: [1, -34],
126     shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
127     shadowSize: [41, 41]
128   });
129 }
130
131 function addObjectToMap(object, zoom, callback) {
132   if (objectLayer) {
133     map.removeLayer(objectLayer);
134   }
135
136   $.ajax({
137     url: OSM.apiUrl(object),
138     dataType: "xml",
139     success: function (xml) {
140       objectLayer = new L.OSM.DataLayer(xml, {
141         style: {
142           strokeColor: "blue",
143           strokeWidth: 3,
144           strokeOpacity: 0.5,
145           fillOpacity: 0.2,
146           fillColor: "lightblue",
147           pointRadius: 6
148         }
149       });
150
151       var bounds = objectLayer.getBounds();
152
153       if (zoom) {
154         map.fitBounds(bounds);
155       }
156
157       if (callback) {
158         callback(bounds);
159       }
160
161       objectLayer.addTo(map);
162     }
163   });
164 }
165
166 function addBoxToMap(bounds) {
167   var box = L.rectangle(bounds, {
168     weight: 2,
169     color: '#e90',
170     fillOpacity: 0
171   });
172
173   box.addTo(map);
174
175   return box;
176 }
177
178 function getMapBaseLayer() {
179   for (var i = 0; i < layers.length; i++) {
180     if (map.hasLayer(layers[i].layer)) {
181       return layers[i];
182     }
183   }
184 }
185
186 function getMapLayers() {
187   for (var i = 0; i < layers.length; i++) {
188     if (map.hasLayer(layers[i].layer)) {
189       return layers[i].layerCode;
190     }
191   }
192
193   return "";
194 }
195
196 function setMapLayers(layerConfig) {
197   var foundLayer = false;
198   for (var i = 0; i < layers.length; i++) {
199     if (layerConfig.indexOf(layers[i].layerCode) >= 0) {
200       map.addLayer(layers[i].layer);
201       foundLayer = true;
202     } else {
203       map.removeLayer(layers[i].layer);
204     }
205   }
206   if (!foundLayer) {
207     map.addLayer(layers[0].layer);
208   }
209 }