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