| 1 |
<?php |
| 2 |
/** |
| 3 |
* Image Shortcode |
| 4 |
* |
| 5 |
* Displays map with [leaflet-image src="path/to/image.jpg"] |
| 6 |
* |
| 7 |
* JavaScript equivalent : L.imageOverlay('path/to/image.jpg'); |
| 8 |
* |
| 9 |
* PHP Version 5.5 |
| 10 |
* |
| 11 |
* @category Shortcode |
| 12 |
* @author Benjamin J DeLong <ben@bozdoz.com> |
| 13 |
*/ |
| 14 |
|
| 15 |
// Exit if accessed directly |
| 16 |
if (!defined('ABSPATH')) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
require_once LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.map-shortcode.php'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Leaflet Image Shortcode Class |
| 24 |
*/ |
| 25 |
class Leaflet_Image_Shortcode extends Leaflet_Map_Shortcode |
| 26 |
{ |
| 27 |
/** |
| 28 |
* Get HTML for shortcode |
| 29 |
* |
| 30 |
* @param string $atts or Array |
| 31 |
* @param string $content produced by adding atts to JavaScript |
| 32 |
* |
| 33 |
* @return string HTML script |
| 34 |
*/ |
| 35 |
protected function getHTML($atts='', $content=null) |
| 36 |
{ |
| 37 |
extract($this->getAtts($atts)); |
| 38 |
|
| 39 |
/* only required field for image map (src/source) */ |
| 40 |
$src = empty($src) ? '' : $src; |
| 41 |
$source = empty($source) ? 'https://picsum.photos/1000/1000/' : $source; |
| 42 |
$source = empty($src) ? $source : $src; |
| 43 |
|
| 44 |
ob_start(); |
| 45 |
?> |
| 46 |
<div |
| 47 |
id="leaflet-map-image-<?php echo $this->map_id; ?>" |
| 48 |
class="leaflet-map" |
| 49 |
style="height:<?php |
| 50 |
echo $height; |
| 51 |
?>; width:<?php |
| 52 |
echo $width; |
| 53 |
?>;"></div> |
| 54 |
<script> |
| 55 |
window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || []; |
| 56 |
window.WPLeafletMapPlugin.push(function () { |
| 57 |
var map, |
| 58 |
options = L.Util.extend({}, { |
| 59 |
maxZoom: <?php echo $max_zoom; ?>, |
| 60 |
minZoom: <?php echo $min_zoom; ?>, |
| 61 |
zoomControl: <?php echo $zoomcontrol; ?>, |
| 62 |
scrollWheelZoom: <?php echo $scrollwheel; ?>, |
| 63 |
doubleClickZoom: <?php echo $doubleclickzoom; ?>, |
| 64 |
attributionControl: false |
| 65 |
}, <?php echo $more_options; ?>, { |
| 66 |
crs: L.CRS.Simple |
| 67 |
}), |
| 68 |
image_src = '<?php echo $source; ?>', |
| 69 |
img = new Image(), |
| 70 |
zoom = <?php echo $zoom; ?>; |
| 71 |
img.onload = function() { |
| 72 |
var h = img.height, |
| 73 |
w = img.width, |
| 74 |
projected_zoom = zoom + 1, |
| 75 |
southWest = map.unproject([-w, h], projected_zoom), |
| 76 |
northEast = map.unproject([w, -h], projected_zoom), |
| 77 |
bounds = new L.LatLngBounds(southWest, northEast); |
| 78 |
L.imageOverlay( image_src, bounds ).addTo( map ); |
| 79 |
map.setMaxBounds(bounds); |
| 80 |
}; |
| 81 |
img.src = image_src; |
| 82 |
map = L.map('leaflet-map-image-<?php echo $this->map_id; ?>', options).setView([0, 0], zoom); |
| 83 |
// make it known that it is an image map |
| 84 |
map.is_image_map = true; |
| 85 |
if (<?php echo $fit_markers; ?>) { |
| 86 |
map.fit_markers = true; |
| 87 |
} |
| 88 |
<?php |
| 89 |
if ($attribution) { |
| 90 |
/* add any attributions, semi-colon-separated */ |
| 91 |
$attributions = explode(';', $attribution); |
| 92 |
?> |
| 93 |
var attControl = L.control.attribution({prefix:false}).addTo(map); |
| 94 |
<?php |
| 95 |
foreach ($attributions as $a) { |
| 96 |
?> |
| 97 |
attControl.addAttribution('<?php echo trim($a); ?>'); |
| 98 |
<?php |
| 99 |
} |
| 100 |
} |
| 101 |
?> |
| 102 |
window.WPLeafletMapPlugin.maps.push( map ); |
| 103 |
window.WPLeafletMapPlugin.images.push( img ); |
| 104 |
}); // end add |
| 105 |
</script> |
| 106 |
<?php |
| 107 |
return ob_get_clean(); |
| 108 |
} |
| 109 |
} |
| 110 |
|