2 //= require leaflet.sidebar
3 //= require leaflet.locate
4 //= require leaflet.layers
5 //= require leaflet.key
6 //= require leaflet.note
7 //= require leaflet.share
8 //= require index/search
9 //= require index/browse
10 //= require index/export
11 //= require index/notes
12 //= require index/history
15 $(document).ready(function () {
16 var params = OSM.mapParams();
18 var map = L.map("map", {
23 map.attributionControl.setPrefix('');
25 map.hash = L.hash(map);
27 $(window).on('popstate', function(e) {
28 // popstate is triggered when the hash changes as well as on actual navigation
29 // events. We want to update the hash on the latter and not the former.
30 if (e.originalEvent.state) {
35 var copyright = I18n.t('javascripts.map.copyright', {copyright_url: '/copyright'});
36 var donate = I18n.t('javascripts.map.donate_link_text', {donate_url: 'http://donate.openstreetmap.org'});
40 attribution: copyright + " ♥ " + donate,
43 name: I18n.t("javascripts.map.base.standard")
46 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
49 name: I18n.t("javascripts.map.base.cycle_map")
51 new L.OSM.TransportMap({
52 attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
54 keyid: "transportmap",
55 name: I18n.t("javascripts.map.base.transport_map")
57 new L.OSM.MapQuestOpen({
58 attribution: copyright + ". Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>",
61 name: I18n.t("javascripts.map.base.mapquest")
64 attribution: copyright + ". Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>",
67 name: I18n.t("javascripts.map.base.hot")
71 function updateLayers(params) {
72 var layerParam = params.layers || "M";
75 for (var i = layers.length - 1; i >= 0; i--) {
76 if (layerParam.indexOf(layers[i].options.code) >= 0) {
77 map.addLayer(layers[i]);
78 layersAdded = layersAdded + layers[i].options.code;
79 } else if (i == 0 && layersAdded == "") {
80 map.addLayer(layers[i]);
82 map.removeLayer(layers[i]);
89 $(window).on("hashchange", function () {
90 updateLayers(OSM.mapParams());
93 map.on("baselayerchange", function (e) {
94 if (map.getZoom() > e.layer.options.maxZoom) {
95 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
99 map.noteLayer = new L.LayerGroup();
100 map.noteLayer.options = {code: 'N'};
102 map.dataLayer = new L.OSM.DataLayer(null);
103 map.dataLayer.options.code = 'D';
105 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
106 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
107 map.addLayer(map.noteLayer);
110 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
111 map.addLayer(map.dataLayer);
115 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
117 L.OSM.zoom({position: position})
123 title: I18n.t('javascripts.map.locate.title'),
124 popup: I18n.t('javascripts.map.locate.popup')
128 var sidebar = L.OSM.sidebar('#map-ui')
156 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
158 map.on('moveend layeradd layerremove', function() {
160 map.getCenter().wrap(),
165 var expiry = new Date();
166 expiry.setYear(expiry.getFullYear() + 10);
167 $.cookie("_osm_location", cookieContent(map), { expires: expiry });
169 // Trigger hash update on layer changes.
170 map.hash.onMapMove();
174 map.on('layeradd', function (e) {
175 if (e.layer.options) {
176 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
179 $('body').trigger('piwikgoal', goal);
186 map.fitBounds(params.bounds);
188 map.setView([params.lat, params.lon], params.zoom);
191 var marker = L.marker([0, 0], {icon: getUserIcon()});
194 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
197 $("#homeanchor").on("click", function(e) {
200 var data = $(this).data(),
201 center = L.latLng(data.lat, data.lon);
203 map.setView(center, data.zoom);
204 marker.setLatLng(center).addTo(map);
207 $("a[data-editor=remote]").click(function(e) {
208 remoteEditHandler(map.getBounds());
212 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
213 remoteEditHandler(map.getBounds());
216 if (OSM.params().edit_help) {
221 title: I18n.t('javascripts.edit_help')
225 $('body').one('click', function() {
226 $('#editanchor').tooltip('hide');
230 initializeBrowse(map);
231 initializeNotes(map);
233 OSM.Index = function(map) {
234 var page = {}, minimized = $('#sidebar').hasClass('minimized');
236 page.pushstate = page.popstate = function(path) {
237 if (minimized) $("#sidebar").addClass("minimized");
238 map.invalidateSize();
239 $('#sidebar_content').load(path);
245 OSM.Browse = function(map) {
248 page.pushstate = page.popstate = function(path, type, id) {
249 $("#sidebar").removeClass("minimized");
250 map.invalidateSize();
251 $('#sidebar_content').load(path, function() {
252 page.load(path, type, id);
256 page.load = function(path, type, id) {
257 $("#sidebar").removeClass("minimized");
259 if (OSM.STATUS === 'api_offline' || OSM.STATUS === 'database_offline') return;
261 if (type === 'note') {
262 map.noteLayer.showNote(parseInt(id));
264 map.addObject({type: type, id: parseInt(id)}, {zoom: true});
268 page.unload = function() {
275 var history = OSM.History(map);
277 OSM.route = OSM.Router({
279 "/search": OSM.Search(map),
280 "/export": OSM.Export(map),
282 "/user/:display_name/edits": history,
283 "/browse/friends": history,
284 "/browse/nearby": history,
285 "/browse/:type/:id(/history)": OSM.Browse(map)
288 $(document).on("click", "a", function(e) {
289 if (e.isDefaultPrevented() || e.isPropagationStopped()) return;
290 if (this.host === window.location.host && OSM.route(this.pathname + this.search + this.hash)) e.preventDefault();
293 $("#search_form").on("submit", function(e) {
295 OSM.route("/search?query=" + encodeURIComponent($("#query").val()) + OSM.formatHash(map));
298 $("#describe_location").on("click", function(e) {
300 var precision = zoomPrecision(map.getZoom());
301 OSM.route("/search?query=" + encodeURIComponent(
302 map.getCenter().lat.toFixed(precision) + "," +
303 map.getCenter().lng.toFixed(precision)));
306 function removeSidebar() {
307 if ($(window).width() < 721) {
308 $('#sidebar').addClass("minimized");
312 $(window).resize(function() {