2 //= require index/browse
3 //= require index/export
5 //= require index/notes
6 //= require index/map_ui
8 var map, layers; // TODO: remove globals
10 $(document).ready(function () {
12 var params = OSM.mapParams();
19 map.attributionControl.setPrefix('');
22 layers[0].layer.addTo(map);
24 $("#map").on("resized", function () {
28 L.control.zoom({position: 'topright'})
31 OSM.mapUI().addTo(map);
34 getUrl: function(map) {
35 return setArgs('http://osm.org/', {
36 lon: map.getCenter().lng,
37 lat: map.getCenter().lat
50 L.control.scale().addTo(map);
52 map.on("moveend layeradd layerremove", updateLocation);
54 if (!params.object_zoom) {
56 var bbox = L.latLngBounds([params.minlat, params.minlon],
57 [params.maxlat, params.maxlon]);
69 map.setView([params.lat, params.lon], params.zoom);
74 setMapLayers(params.layers);
78 marker = L.marker([params.mlat, params.mlon], {icon: getUserIcon()}).addTo(map);
82 addObjectToMap(params.object, map, { zoom: params.object_zoom });
87 $("body").on("click", "a.set_position", function (e) {
90 var data = $(this).data();
91 var centre = L.latLng(data.lat, data.lon);
93 if (data.minLon && data.minLat && data.maxLon && data.maxLat) {
94 map.fitBounds([[data.minLat, data.minLon],
95 [data.maxLat, data.maxLon]]);
97 map.setView(centre, data.zoom);
100 if (data.type && data.id) {
101 addObjectToMap(data, map, { zoom: true, style: { opacity: 0.2, fill: false } });
105 map.removeLayer(marker);
108 marker = L.marker(centre, {icon: getUserIcon()}).addTo(map);
111 function updateLocation() {
112 var center = map.getCenter().wrap();
113 var zoom = map.getZoom();
114 var layers = getMapLayers();
115 var extents = map.getBounds().wrap();
117 updatelinks(center, zoom, layers, extents, params.object);
119 var expiry = new Date();
120 expiry.setYear(expiry.getFullYear() + 10);
121 $.cookie("_osm_location", [center.lng, center.lat, zoom, layers].join("|"), {expires: expiry});
124 function remoteEditHandler() {
125 var extent = map.getBounds();
128 $("#linkloader").load(function () { loaded = true; });
129 $("#linkloader").attr("src", "http://127.0.0.1:8111/load_and_zoom?left=" + extent.getWest()
130 + "&bottom=" + extent.getSouth()
131 + "&right=" + extent.getEast()
132 + "&top=" + extent.getNorth());
134 setTimeout(function () {
135 if (!loaded) alert(I18n.t('site.index.remote_failed'));
141 $("a[data-editor=remote]").click(remoteEditHandler);
143 if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
147 $(window).resize(handleResize);
149 $("#search_form").submit(function () {
150 var bounds = map.getBounds();
152 $("#sidebar_title").html(I18n.t('site.sidebar.search_results'));
153 $("#sidebar_content").load($(this).attr("action"), {
154 query: $("#query").val(),
155 minlon: bounds.getWest(),
156 minlat: bounds.getSouth(),
157 maxlon: bounds.getEast(),
158 maxlat: bounds.getNorth()
164 if ($("#query").val()) {
165 $("#search_form").submit();
168 // Focus the search field for browsers that don't support
169 // the HTML5 'autofocus' attribute
170 if (!("autofocus" in document.createElement("input"))) {
175 function getMapBaseLayer() {
176 for (var i = 0; i < layers.length; i++) {
177 if (map.hasLayer(layers[i].layer)) {
183 function getMapLayers() {
184 var layerConfig = "";
185 for (var i = 0; i < layers.length; i++) {
186 if (map.hasLayer(layers[i].layer)) {
187 layerConfig += layers[i].layerCode;
193 function setMapLayers(layerConfig) {
194 var foundLayer = false;
195 for (var i = 0; i < layers.length; i++) {
196 if (layerConfig.indexOf(layers[i].layerCode) >= 0) {
197 map.addLayer(layers[i].layer);
200 map.removeLayer(layers[i].layer);
204 map.addLayer(layers[0].layer);