2 //= require leaflet.customzoom
3 //= require leaflet.locate
4 //= require leaflet.layers
5 //= require leaflet.key
6 //= require leaflet.note
7 //= require leaflet.share
8 //= require index/browse
9 //= require index/export
10 //= require index/notes
12 $(document).ready(function () {
13 var params = OSM.mapParams();
15 var map = L.map("map", {
20 map.attributionControl.setPrefix('');
27 name: I18n.t("javascripts.map.base.standard")
30 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
33 name: I18n.t("javascripts.map.base.cycle_map")
35 new L.OSM.TransportMap({
36 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
38 keyid: "transportmap",
39 name: I18n.t("javascripts.map.base.transport_map")
41 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'>",
45 name: I18n.t("javascripts.map.base.mapquest")
51 map.noteLayer = new L.LayerGroup({code: 'N'});
52 map.dataLayer = new L.OSM.DataLayer(null);
54 $("#map").on("resized", function () {
58 L.control.customZoom({position: 'topright'})
61 L.control.locate({position: 'topright'})
64 var uiPane = $('#map-ui')[0];
90 map.on('moveend layeradd layerremove', updateLocation);
92 map.markerLayer = L.layerGroup().addTo(map);
94 if (!params.object_zoom) {
96 var bbox = L.latLngBounds([params.minlat, params.minlon],
97 [params.maxlat, params.maxlon]);
109 map.setView([params.lat, params.lon], params.zoom);
114 var foundLayer = false;
115 for (var i = 0; i < layers.length; i++) {
116 if (params.layers.indexOf(layers[i].options.code) >= 0) {
117 map.addLayer(layers[i]);
120 map.removeLayer(layers[i]);
124 map.addLayer(layers[0]);
129 L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map.markerLayer);
133 addObjectToMap(params.object, map, { zoom: params.object_zoom });
138 $("body").on("click", "a.set_position", setPositionLink(map));
140 $("a[data-editor=remote]").click(function(e) {
141 remoteEditHandler(map.getBounds());
145 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
146 remoteEditHandler(map.getBounds());
149 $(window).resize(handleResize);
151 $("#search_form").submit(submitSearch(map));
154 if ($("#query").val()) {
155 $("#search_form").submit();
158 // Focus the search field for browsers that don't support
159 // the HTML5 'autofocus' attribute
160 if (!("autofocus" in document.createElement("input"))) {
164 initializeBrowse(map);
165 initializeNotes(map);
168 function getMapBaseLayerId(map) {
169 for (var i in map._layers) { // TODO: map.eachLayer
170 var layer = map._layers[i];
171 if (layer.options && layer.options.keyid) {
172 return layer.options.keyid;
177 function getMapLayers(map) {
178 var layerConfig = '';
179 for (var i in map._layers) { // TODO: map.eachLayer
180 var layer = map._layers[i];
181 if (layer.options && layer.options.code) {
182 layerConfig += layer.options.code;
188 // generate a cookie-safe string of map state
189 function cookieContent(map) {
190 var center = map.getCenter().wrap();
191 return [center.lng, center.lat, map.getZoom(), getMapLayers(map)].join('|');
194 function updateLocation() {
195 updatelinks(this.getCenter().wrap(),
198 this.getBounds().wrap(), {});
200 var expiry = new Date();
201 expiry.setYear(expiry.getFullYear() + 10);
202 $.cookie("_osm_location", cookieContent(this), { expires: expiry });
205 function setPositionLink(map) {
207 var data = $(this).data(),
208 center = L.latLng(data.lat, data.lon);
210 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
211 map.fitBounds([[data.minLat, data.minLon],
212 [data.maxLat, data.maxLon]]);
214 map.setView(center, data.zoom);
217 if (data.type && data.id) {
218 addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
221 map.markerLayer.clearLayers();
222 L.marker(center, {icon: getUserIcon()}).addTo(map.markerLayer);
224 return e.preventDefault();
228 function submitSearch(map) {
230 var bounds = map.getBounds();
232 $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
233 $("#sidebar_content").load($(this).attr("action"), {
234 query: $("#query").val(),
235 minlon: bounds.getWestLng(),
236 minlat: bounds.getSouthLat(),
237 maxlon: bounds.getEastLng(),
238 maxlat: bounds.getNorthLat()
241 return e.preventDefault();