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')));
64 var $shortLink, $longLink;
67 .appendTo($linkSection)
69 .append($longLink = $('<a>')
70 .text(I18n.t('javascripts.share.long_link'))))
72 .append($shortLink = $('<a>')
73 .text(I18n.t('javascripts.share.short_link'))));
77 var $embedSection = $('<div>')
78 .attr('class', 'section share-html')
82 .text(I18n.t('javascripts.share.embed'))
83 .appendTo($embedSection);
86 .attr('class', 'standard-form')
87 .appendTo($embedSection);
90 .attr('class', 'form-row')
94 .attr('for', 'embed_marker')
97 .attr('id', 'embed_marker')
98 .attr('type', 'checkbox')
99 .bind('change', toggleMarker))
100 .append(I18n.t('javascripts.share.include_marker')));
103 .attr('class', 'form-row')
107 .attr('id', 'embed_html')
108 .on('click', select));
111 .attr('class', 'deemphasize')
112 .text(I18n.t('javascripts.share.paste_html'))
113 .appendTo($embedSection);
117 var $imageSection = $('<div>')
118 .attr('class', 'section share-image')
122 .text(I18n.t('javascripts.share.image'))
123 .appendTo($imageSection);
126 .attr('class', 'standard-form')
127 .attr('action', '/export/finish')
128 .attr('method', 'post')
129 .appendTo($imageSection);
132 .attr('class', 'form-row')
136 .attr('for', 'image_filter')
139 .attr('id', 'image_filter')
140 .attr('type', 'checkbox')
141 .bind('change', toggleFilter))
142 .append(I18n.t('javascripts.share.custom_dimensions')));
145 .attr('class', 'form-row')
149 .attr('class', 'standard-label')
150 .attr('for', 'mapnik_format')
151 .text(I18n.t('javascripts.share.format')))
152 .append($('<select>')
153 .attr('name', 'mapnik_format')
154 .attr('id', 'mapnik_format')
155 .append($('<option>').val('png').text('PNG').prop('selected', true))
156 .append($('<option>').val('jpeg').text('JPEG'))
157 .append($('<option>').val('svg').text('SVG'))
158 .append($('<option>').val('pdf').text('PDF')));
161 .attr('class', 'form-row')
164 .attr('class', 'standard-label')
165 .attr('for', 'mapnik_scale')
166 .text(I18n.t('javascripts.share.scale')))
169 .attr('name', 'mapnik_scale')
170 .attr('id', 'mapnik_scale')
171 .attr('type', 'text')
172 .on('change', update));
174 ['minlon', 'minlat', 'maxlon', 'maxlat'].forEach(function(name) {
176 .attr('id', 'mapnik_' + name)
178 .attr('type', 'hidden')
183 .attr('name', 'format')
184 .attr('value', 'mapnik')
185 .attr('type', 'hidden')
189 .attr('class', 'deemphasize')
190 .html(I18n.t('javascripts.share.image_size') + ' <span id="mapnik_image_width"></span> x <span id="mapnik_image_height"></span>')
194 .attr('type', 'submit')
195 .attr('value', I18n.t('javascripts.share.download'))
199 .on('change', update)
202 map.on('moveend layeradd layerremove', update);
203 marker.on('dragend', update);
205 options.sidebar.addPane($ui);
211 map.removeLayer(marker);
212 locationFilter.disable();
220 $('#mapnik_scale').val(getScale());
221 marker.setLatLng(map.getCenter());
224 options.sidebar.togglePane($ui);
227 function toggleMarker() {
228 if ($(this).is(':checked')) {
229 map.addLayer(marker);
231 map.removeLayer(marker);
236 function toggleFilter() {
237 if ($(this).is(':checked')) {
238 if (!locationFilter.getBounds().isValid()) {
239 locationFilter.setBounds(map.getBounds().pad(-0.2));
242 locationFilter.enable();
244 locationFilter.disable();
250 var bounds = map.getBounds();
252 $('#link_marker, #embed_marker')
253 .prop('checked', map.hasLayer(marker));
257 $shortLink.attr('href', map.getShortUrl(marker));
258 $longLink.attr('href', map.getUrl(marker));
263 bbox: bounds.toBBoxString(),
264 layer: map.getMapBaseLayerId()
267 if (map.hasLayer(marker)) {
268 params.marker = marker.getLatLng().lat + ',' + marker.getLatLng().lng;
271 $('#embed_html').val(
272 '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' +
273 'http://' + OSM.SERVER_URL + '/export/embed.html?' + $.param(params) +
274 '" style="border: 1px solid black"></iframe><br/>' +
275 '<small><a href="' + map.getUrl(marker) + '</a></small>');
279 if (locationFilter.isEnabled()) {
280 bounds = locationFilter.getBounds();
283 var scale = $("#mapnik_scale").val(),
284 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
285 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
286 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
288 $('#mapnik_minlon').val(bounds.getWest());
289 $('#mapnik_minlat').val(bounds.getSouth());
290 $('#mapnik_maxlon').val(bounds.getEast());
291 $('#mapnik_maxlat').val(bounds.getNorth());
293 if (scale < maxScale) {
294 scale = roundScale(maxScale);
295 $("#mapnik_scale").val(scale);
298 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
299 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
306 function getScale() {
307 var bounds = map.getBounds(),
308 centerLat = bounds.getCenter().lat,
309 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
310 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
311 pixelsPerMeter = map.getSize().x / meters,
312 metersPerPixel = 1 / (92 * 39.3701);
313 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
316 function roundScale(scale) {
317 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
318 return precision * Math.ceil(scale / precision);
321 return $container[0];