]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
70801306be7bc43f5f56e1710e74be9c75e93982
[rails.git] / app / assets / javascripts / map.js.erb
1 // Leaflet extensions
2 L.extend(L.LatLngBounds.prototype, {
3   getSize: function () {
4     return (this._northEast.lat - this._southWest.lat) *
5            (this._northEast.lng - this._southWest.lng);
6   },
7
8   wrap: function () {
9     return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
10   }
11 });
12
13 L.Icon.Default.imagePath = <%= "#{asset_prefix}/images".to_json %>;
14
15 var objectLayer;
16 var objectLoader;
17
18 function getUserIcon(url) {
19   return L.icon({
20     iconUrl: url || <%= asset_path('marker-red.png').to_json %>,
21     iconSize: [25, 41],
22     iconAnchor: [12, 41],
23     popupAnchor: [1, -34],
24     shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
25     shadowSize: [41, 41]
26   });
27 }
28
29 function addObjectToMap(object, map, options) {
30   if (objectLoader) objectLoader.abort();
31   if (objectLayer) map.removeLayer(objectLayer);
32
33   objectLoader = $.ajax({
34     url: OSM.apiUrl(object),
35     dataType: "xml",
36     success: function (xml) {
37       objectLayer = new L.OSM.DataLayer(null, {
38         styles: {
39           node: options.style,
40           way: options.style,
41           area: options.style
42         }
43       });
44
45       objectLayer.interestingNode = function (node, ways, relations) {
46         if (object.type === "node") {
47           return true;
48         } else if (object.type === "relation") {
49           for (var i = 0; i < relations.length; i++)
50             if (relations[i].members.indexOf(node) != -1)
51               return true;
52         } else {
53           return false;
54         }
55       };
56
57       objectLayer.addData(xml);
58
59       if (options.zoom) map.fitBounds(objectLayer.getBounds());
60       if (options.callback) options.callback(objectLayer.getBounds());
61
62       objectLayer.addTo(map);
63     }
64   });
65 }