]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/map.js.erb
323d16378479ff78adb0d3cb1043ffc89c8a8b4a
[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 mapLayers() {
19   return [{
20     layer: new L.OSM.Mapnik(),
21     keyid: "mapnik",
22     layerCode: "M",
23     name: I18n.t("javascripts.map.base.standard")
24   }, {
25     layer: new L.OSM.CycleMap( {
26         attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
27     }),
28     keyid: "cyclemap",
29     layerCode: "C",
30     name: I18n.t("javascripts.map.base.cycle_map")
31   }, {
32     layer: new L.OSM.TransportMap({
33         attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
34     }),
35     keyid: "transportmap",
36     layerCode: "T",
37     name: I18n.t("javascripts.map.base.transport_map")
38   }, {
39     layer: new L.OSM.MapQuestOpen({
40         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'>",
41     }),
42     keyid: "mapquest",
43     layerCode: "Q",
44     name: I18n.t("javascripts.map.base.mapquest")
45   }]
46 }
47
48 function getUserIcon(url) {
49   return L.icon({
50     iconUrl: url || <%= asset_path('marker-red.png').to_json %>,
51     iconSize: [25, 41],
52     iconAnchor: [12, 41],
53     popupAnchor: [1, -34],
54     shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
55     shadowSize: [41, 41]
56   });
57 }
58
59 function addObjectToMap(object, map, options) {
60   if (objectLoader) {
61     objectLoader.abort();
62   }
63
64   if (objectLayer) {
65     map.removeLayer(objectLayer);
66   }
67
68   objectLoader = $.ajax({
69     url: OSM.apiUrl(object),
70     dataType: "xml",
71     success: function (xml) {
72       objectLayer = new L.OSM.DataLayer(null, {
73         styles: {
74           node: options.style,
75           way: options.style,
76           area: options.style
77         }
78       });
79
80       objectLayer.interestingNode = function (node, ways, relations) {
81         if (object.type === "node") {
82           return true;
83         } else if (object.type === "relation") {
84           for (var i = 0; i < relations.length; i++)
85             if (relations[i].members.indexOf(node) != -1)
86               return true;
87         } else {
88           return false;
89         }
90       };
91
92       objectLayer.addData(xml);
93
94       var bounds = objectLayer.getBounds();
95
96       if (options.zoom) {
97         map.fitBounds(bounds);
98       }
99
100       if (options.callback) {
101         options.callback(bounds);
102       }
103
104       objectLayer.addTo(map);
105     }
106   });
107 }