2 //= require leaflet.layers
3 //= require leaflet.share
4 //= require leaflet.note
5 //= require leaflet.locate
6 //= require index/browse
7 //= require index/export
9 //= require index/notes
11 var map, layers; // TODO: remove globals
13 $(document).ready(function () {
15 var params = OSM.mapParams();
22 map.attributionControl.setPrefix('');
25 layer: new L.OSM.Mapnik({
30 name: I18n.t("javascripts.map.base.standard")
32 layer: new L.OSM.CycleMap( {
33 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
37 name: I18n.t("javascripts.map.base.cycle_map")
39 layer: new L.OSM.TransportMap({
40 attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
42 keyid: "transportmap",
44 name: I18n.t("javascripts.map.base.transport_map")
46 layer: new L.OSM.MapQuestOpen({
47 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'>",
51 name: I18n.t("javascripts.map.base.mapquest")
54 layers[0].layer.addTo(map);
56 $("#map").on("resized", function () {
60 L.control.zoom({position: 'topright'})
63 L.OSM.layers({position: 'topright', layers: layers})
78 L.control.scale().addTo(map);
80 map.on("moveend layeradd layerremove", updateLocation);
82 if (!params.object_zoom) {
84 var bbox = L.latLngBounds([params.minlat, params.minlon],
85 [params.maxlat, params.maxlon]);
97 map.setView([params.lat, params.lon], params.zoom);
102 setMapLayers(params.layers);
106 marker = L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map);
110 addObjectToMap(params.object, map, { zoom: params.object_zoom });
115 $("body").on("click", "a.set_position", function (e) {
118 var data = $(this).data();
119 var centre = L.latLng(data.lat, data.lon);
121 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
122 map.fitBounds([[data.minLat, data.minLon],
123 [data.maxLat, data.maxLon]]);
125 map.setView(centre, data.zoom);
128 if (data.type && data.id) {
129 addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
133 map.removeLayer(marker);
136 marker = L.marker(centre, {icon: getUserIcon()}).addTo(map);
139 function updateLocation() {
140 var center = map.getCenter().wrap();
141 var zoom = map.getZoom();
142 var layers = getMapLayers();
143 var extents = map.getBounds().wrap();
145 updatelinks(center, zoom, layers, extents, params.object);
147 var expiry = new Date();
148 expiry.setYear(expiry.getFullYear() + 10);
149 $.cookie("_osm_location", [center.lng, center.lat, zoom, layers].join("|"), {expires: expiry});
152 function remoteEditHandler() {
153 var extent = map.getBounds();
156 $("#linkloader").load(function () { loaded = true; });
157 $("#linkloader").attr("src", "http://127.0.0.1:8111/load_and_zoom?" +
158 querystring.stringify({
159 left: extent.getWest(),
160 bottom: extent.getSouth(),
161 right: extent.getEast(),
162 top: extent.getNorth()
165 setTimeout(function () {
166 if (!loaded) alert(I18n.t('site.index.remote_failed'));
172 $("a[data-editor=remote]").click(remoteEditHandler);
174 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
178 $(window).resize(handleResize);
180 $("#search_form").submit(function () {
181 var bounds = map.getBounds();
183 $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
184 $("#sidebar_content").load($(this).attr("action"), {
185 query: $("#query").val(),
186 minlon: bounds.getWest(),
187 minlat: bounds.getSouth(),
188 maxlon: bounds.getEast(),
189 maxlat: bounds.getNorth()
195 if ($("#query").val()) {
196 $("#search_form").submit();
199 // Focus the search field for browsers that don't support
200 // the HTML5 'autofocus' attribute
201 if (!("autofocus" in document.createElement("input"))) {
206 function getMapBaseLayer() {
207 for (var i = 0; i < layers.length; i++) {
208 if (map.hasLayer(layers[i].layer)) {
214 function getMapLayers() {
215 var layerConfig = "";
216 for (var i = 0; i < layers.length; i++) {
217 if (map.hasLayer(layers[i].layer)) {
218 layerConfig += layers[i].layerCode;
224 function setMapLayers(layerConfig) {
225 var foundLayer = false;
226 for (var i = 0; i < layers.length; i++) {
227 if (layerConfig.indexOf(layers[i].layerCode) >= 0) {
228 map.addLayer(layers[i].layer);
231 map.removeLayer(layers[i].layer);
235 map.addLayer(layers[0].layer);