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