1 L.OSM.share = function (options) {
2 var control = L.control(options),
3 marker = L.marker([0, 0], {draggable: true}),
4 locationFilter = new L.LocationFilter({
9 control.onAdd = function (map) {
10 var $container = $('<div>')
11 .attr('class', 'control-share');
14 .attr('class', 'control-button')
16 .attr('title', 'Share')
17 .html('<span class="icon share"></span>')
19 .appendTo($container);
22 .attr('class', 'share-ui');
25 .attr('class', 'sidebar_heading')
29 .text(I18n.t('javascripts.close'))
30 .attr('class', 'sidebar_close')
32 .bind('click', toggle))
35 .text(I18n.t('javascripts.share.title')));
39 var $linkSection = $('<div>')
40 .attr('class', 'section share-link')
44 .text(I18n.t('javascripts.share.link'))
45 .appendTo($linkSection);
47 var $form = $('<form>')
48 .attr('class', 'standard-form')
49 .appendTo($linkSection);
52 .attr('class', 'form-row')
56 .attr('for', 'link_marker')
59 .attr('id', 'link_marker')
60 .attr('type', 'checkbox')
61 .bind('change', toggleMarker))
62 .append(I18n.t('javascripts.share.include_marker')));
65 .attr('class', 'form-row')
69 .attr('for', 'center_marker')
72 .attr('id', 'center_marker')
73 .attr('type', 'checkbox')
74 .prop('checked', true)
75 .bind('change', update))
76 .append(I18n.t('javascripts.share.center_marker')));
79 .attr('class', 'form-row')
82 .attr('for', 'long_input')
83 .text(I18n.t('javascripts.share.long_link')))
85 .attr('id', 'long_link')
87 .attr('class', 'icon link')))
89 .attr('id', 'long_input')
91 .on('click', select));
94 .attr('class', 'form-row')
97 .attr('for', 'short_input')
98 .text(I18n.t('javascripts.share.short_link')))
100 .attr('id', 'short_link')
102 .attr('class', 'icon link')))
104 .attr('id', 'short_input')
105 .attr('type', 'text')
106 .on('click', select));
110 var $embedSection = $('<div>')
111 .attr('class', 'section share-embed')
114 var $form = $('<form>')
115 .attr('class', 'standard-form')
116 .appendTo($embedSection);
119 .attr('class', 'form-row')
122 .attr('for', 'embed_html')
123 .text(I18n.t('javascripts.share.embed')))
126 .attr('id', 'embed_html')
127 .on('click', select));
130 .attr('class', 'deemphasize')
131 .text(I18n.t('javascripts.share.paste_html'))
132 .appendTo($linkSection);
136 var $imageSection = $('<div>')
137 .attr('class', 'section share-image')
141 .text(I18n.t('javascripts.share.image'))
142 .appendTo($imageSection);
145 .attr('class', 'standard-form')
146 .attr('action', '/export/finish')
147 .attr('method', 'post')
148 .appendTo($imageSection);
151 .attr('class', 'form-row')
155 .attr('for', 'image_filter')
158 .attr('id', 'image_filter')
159 .attr('type', 'checkbox')
160 .bind('change', toggleFilter))
161 .append(I18n.t('javascripts.share.custom_dimensions')));
164 .attr('class', 'form-row')
168 .attr('class', 'standard-label')
169 .attr('for', 'mapnik_format')
170 .text(I18n.t('javascripts.share.format')))
171 .append($('<select>')
172 .attr('name', 'mapnik_format')
173 .attr('id', 'mapnik_format')
174 .append($('<option>').val('png').text('PNG').prop('selected', true))
175 .append($('<option>').val('jpeg').text('JPEG'))
176 .append($('<option>').val('svg').text('SVG'))
177 .append($('<option>').val('pdf').text('PDF')));
180 .attr('class', 'form-row')
183 .attr('class', 'standard-label')
184 .attr('for', 'mapnik_scale')
185 .text(I18n.t('javascripts.share.scale')))
188 .attr('name', 'mapnik_scale')
189 .attr('id', 'mapnik_scale')
190 .attr('type', 'text')
191 .on('change', update));
193 ['minlon', 'minlat', 'maxlon', 'maxlat'].forEach(function(name) {
195 .attr('id', 'mapnik_' + name)
197 .attr('type', 'hidden')
202 .attr('name', 'format')
203 .attr('value', 'mapnik')
204 .attr('type', 'hidden')
208 .attr('class', 'deemphasize')
209 .html(I18n.t('javascripts.share.image_size') + ' <span id="mapnik_image_width"></span> x <span id="mapnik_image_height"></span>')
213 .attr('type', 'submit')
214 .attr('value', I18n.t('javascripts.share.download'))
218 .on('change', update)
221 map.on('moveend layeradd layerremove', update);
222 marker.on('dragend', update);
224 options.sidebar.addPane($ui);
230 map.removeLayer(marker);
231 locationFilter.disable();
239 $('#mapnik_scale').val(getScale());
240 marker.setLatLng(map.getCenter());
243 options.sidebar.togglePane($ui);
246 function toggleMarker() {
247 if ($(this).is(':checked')) {
248 marker.setLatLng(map.getCenter());
249 map.addLayer(marker);
251 map.removeLayer(marker);
256 function toggleFilter() {
257 if ($(this).is(':checked')) {
258 if (!locationFilter.getBounds().isValid()) {
259 locationFilter.setBounds(map.getBounds().pad(-0.2));
262 locationFilter.enable();
264 locationFilter.disable();
270 if (map.hasLayer(marker) && $('#center_marker').is(':checked')) {
271 map.panTo(marker.getLatLng());
274 var bounds = map.getBounds();
277 .prop('checked', map.hasLayer(marker));
280 .prop('disabled', !map.hasLayer(marker));
283 .prop('checked', locationFilter.isEnabled());
287 $('#short_input').val(map.getShortUrl(marker));
288 $('#long_input').val(map.getUrl(marker));
289 $('#short_link').attr('href', map.getShortUrl(marker));
290 $('#long_link').attr('href', map.getUrl(marker));
293 bbox: bounds.toBBoxString(),
294 layer: map.getMapBaseLayerId()
297 if (map.hasLayer(marker)) {
298 params.marker = marker.getLatLng().lat + ',' + marker.getLatLng().lng;
301 $('#embed_html').val(
302 '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' +
303 escapeHTML('http://' + OSM.SERVER_URL + '/export/embed.html?' + $.param(params)) +
304 '" style="border: 1px solid black"></iframe><br/>' +
305 '<small><a href="' + escapeHTML(map.getUrl(marker)) + '</a></small>');
309 if (locationFilter.isEnabled()) {
310 bounds = locationFilter.getBounds();
313 var scale = $("#mapnik_scale").val(),
314 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
315 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
316 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
318 $('#mapnik_minlon').val(bounds.getWest());
319 $('#mapnik_minlat').val(bounds.getSouth());
320 $('#mapnik_maxlon').val(bounds.getEast());
321 $('#mapnik_maxlat').val(bounds.getNorth());
323 if (scale < maxScale) {
324 scale = roundScale(maxScale);
325 $("#mapnik_scale").val(scale);
328 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
329 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
336 function getScale() {
337 var bounds = map.getBounds(),
338 centerLat = bounds.getCenter().lat,
339 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
340 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
341 pixelsPerMeter = map.getSize().x / meters,
342 metersPerPixel = 1 / (92 * 39.3701);
343 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
346 function roundScale(scale) {
347 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
348 return precision * Math.ceil(scale / precision);
351 return $container[0];