2 //= require leaflet.layers
3 //= require leaflet.share
4 //= require leaflet.note
5 //= require leaflet.locate
6 //= require leaflet.customzoom
7 //= require index/browse
8 //= require index/export
10 //= require index/notes
12 var map, layers; // TODO: remove globals
14 $(document).ready(function () {
16 var params = OSM.mapParams();
23 map.attributionControl.setPrefix('');
26 layer: new L.OSM.Mapnik({
31 name: I18n.t("javascripts.map.base.standard")
33 layer: new L.OSM.CycleMap({
34 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
38 name: I18n.t("javascripts.map.base.cycle_map")
40 layer: new L.OSM.TransportMap({
41 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
44 keyid: "transportmap",
45 name: I18n.t("javascripts.map.base.transport_map")
47 layer: new L.OSM.MapQuestOpen({
48 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'>",
52 name: I18n.t("javascripts.map.base.mapquest")
55 layers[0].layer.addTo(map);
57 $("#map").on("resized", function () {
61 L.control.customZoom({position: 'topright'})
64 L.OSM.layers({position: 'topright', layers: layers})
79 L.control.scale().addTo(map);
81 map.on("moveend layeradd layerremove", updateLocation);
83 if (!params.object_zoom) {
85 var bbox = L.latLngBounds([params.minlat, params.minlon],
86 [params.maxlat, params.maxlon]);
98 map.setView([params.lat, params.lon], params.zoom);
103 var foundLayer = false;
104 for (var i = 0; i < layers.length; i++) {
105 if (params.layers.indexOf(layers[i].layer.options.code) >= 0) {
106 map.addLayer(layers[i].layer);
109 map.removeLayer(layers[i].layer);
113 map.addLayer(layers[0].layer);
118 marker = L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map);
122 addObjectToMap(params.object, map, { zoom: params.object_zoom });
127 $("body").on("click", "a.set_position", function (e) {
130 var data = $(this).data();
131 var centre = L.latLng(data.lat, data.lon);
133 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
134 map.fitBounds([[data.minLat, data.minLon],
135 [data.maxLat, data.maxLon]]);
137 map.setView(centre, data.zoom);
140 if (data.type && data.id) {
141 addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
145 map.removeLayer(marker);
148 marker = L.marker(centre, {icon: getUserIcon()}).addTo(map);
151 function updateLocation() {
152 var center = map.getCenter().wrap();
153 var zoom = map.getZoom();
154 var layers = getMapLayers();
155 var extents = map.getBounds().wrap();
157 updatelinks(center, zoom, layers, extents, params.object);
159 var expiry = new Date();
160 expiry.setYear(expiry.getFullYear() + 10);
161 $.cookie("_osm_location", [center.lng, center.lat, zoom, layers].join("|"), {expires: expiry});
164 function remoteEditHandler() {
165 var extent = map.getBounds();
168 $("#linkloader").load(function () { loaded = true; });
169 $("#linkloader").attr("src", "http://127.0.0.1:8111/load_and_zoom?" +
170 querystring.stringify({
171 left: extent.getWest(),
172 bottom: extent.getSouth(),
173 right: extent.getEast(),
174 top: extent.getNorth()
177 setTimeout(function () {
178 if (!loaded) alert(I18n.t('site.index.remote_failed'));
184 $("a[data-editor=remote]").click(remoteEditHandler);
186 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
190 $(window).resize(handleResize);
192 $("#search_form").submit(function () {
193 var bounds = map.getBounds();
195 $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
196 $("#sidebar_content").load($(this).attr("action"), {
197 query: $("#query").val(),
198 minlon: bounds.getWest(),
199 minlat: bounds.getSouth(),
200 maxlon: bounds.getEast(),
201 maxlat: bounds.getNorth()
207 if ($("#query").val()) {
208 $("#search_form").submit();
211 // Focus the search field for browsers that don't support
212 // the HTML5 'autofocus' attribute
213 if (!("autofocus" in document.createElement("input"))) {
218 function getMapBaseLayer() {
219 for (var i = 0; i < layers.length; i++) {
220 if (map.hasLayer(layers[i].layer)) {
226 function getMapLayers() {
227 var layerConfig = "";
228 for (var i in map._layers) { // TODO: map.eachLayer
229 var layer = map._layers[i];
230 if (layer.options && layer.options.code) {
231 layerConfig += layer.options.code;