Skip to content

Commit

Permalink
fix to image map; added attributes from map shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
bozdoz committed Aug 29, 2017
1 parent 99a8e7f commit 5845a93
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
2 changes: 1 addition & 1 deletion class.plugin-option.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function __construct ($details = array()) {
}

$option_filter = array(
'default' => FILTER_SANITIZE_STRING,
'default' => null,
'type' => FILTER_SANITIZE_STRING,
'options' => array(
'filter' => FILTER_SANITIZE_STRING,
Expand Down
2 changes: 1 addition & 1 deletion class.plugin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Leaflet_Map_Plugin_Settings {
'css_url' => array(
'default'=>'https://unpkg.com/leaflet@%s/dist/leaflet.css',
'type' => 'text',
'helptext' => 'Save as above.'
'helptext' => 'Same as above.'
),
'default_attribution' => array(
'default' => "<a href=\"http://leafletjs.com\" title=\"A JS library for interactive maps\">Leaflet</a>; \r\n© <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors",
Expand Down
47 changes: 34 additions & 13 deletions shortcodes/class.image-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,50 @@ class="leaflet-map"
<script>
WPLeafletMapPlugin.add(function () {
var map,
options = L.Util.extend({}, {
maxZoom: <?php echo $max_zoom; ?>,
minZoom: <?php echo $min_zoom; ?>,
zoomControl: <?php echo $zoomcontrol; ?>,
scrollWheelZoom: <?php echo $scrollwheel; ?>,
doubleClickZoom: <?php echo $doubleclickzoom; ?>,
attributionControl: false
}, <?php echo $more_options; ?>, {
crs: L.CRS.Simple
}),
image_src = '<?php echo $source; ?>',
img = new Image(),
zoom = <?php echo $zoom; ?>;
img.onload = function() {
var h = img.height;
var w = img.width;
var southWest = map.unproject([-h, h], zoom);
var northEast = map.unproject([w, -w], zoom);
var bounds = new L.LatLngBounds(southWest, northEast);
var h = img.height,
w = img.width,
projected_zoom = zoom + 1,
southWest = map.unproject([-w, h], projected_zoom),
northEast = map.unproject([w, -h], projected_zoom),
bounds = new L.LatLngBounds(southWest, northEast);
L.imageOverlay( image_src, bounds ).addTo( map );
map.setMaxBounds(bounds);
img.is_loaded = true;
};
img.src = image_src;
map = L.map('leaflet-map-image-<?php echo $this->map_id; ?>', {
maxZoom: <?php echo $max_zoom; ?>,
minZoom: <?php echo $min_zoom; ?>,
crs: L.CRS.Simple,
zoomControl: <?php echo $zoomcontrol; ?>,
scrollWheelZoom: <?php echo $scrollwheel; ?>
}).setView([0, 0], zoom);
map = L.map('leaflet-map-image-<?php echo $this->map_id; ?>', options).setView([0, 0], zoom);
// make it known that it is an image map
map.is_image_map = true;
if (<?php echo $fit_markers; ?>) {
map.fit_markers = true;
}
<?php
if ($attribution) {
/* add any attributions, semi-colon-separated */
$attributions = explode(';', $attribution);
?>
var attControl = L.control.attribution({prefix:false}).addTo(map);
<?php
foreach ($attributions as $a) {
?>
attControl.addAttribution('<?php echo trim($a); ?>');
<?php
}
}
?>
WPLeafletMapPlugin.maps.push( map );
WPLeafletMapPlugin.images.push( img );
}); // end add
Expand Down
40 changes: 24 additions & 16 deletions shortcodes/class.map-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,41 @@ protected function incrementMap () {
$this->map_id = $this->LM->map_count;
}

protected function getAtts ($atts, $content = null) {
if ($atts) extract($atts);
/**
* Merge shortcode options with default options
*
* @param array|string $atts key value pairs from shortcode
* @param string $content inner text in shortcode
* @return array new atts, which is actually an array
*/

protected function getAtts ($atts = '', $content = null) {
$atts = (array) $atts;
extract($atts);

$settings = Leaflet_Map_Plugin_Settings::init();

$atts['zoom'] = empty($zoom) ? $settings->get('default_zoom') : $zoom;
$atts['zoom'] = array_key_exists('zoom', $atts) ? $zoom : $settings->get('default_zoom');
$atts['height'] = empty($height) ? $settings->get('default_height') : $height;
$atts['width'] = empty($width) ? $settings->get('default_width') : $width;
$atts['zoomcontrol'] = empty($zoomcontrol) ? $settings->get('show_zoom_controls') : $zoomcontrol;
$atts['min_zoom'] = empty($min_zoom) ? $settings->get('default_min_zoom') : $min_zoom;
$atts['zoomcontrol'] = array_key_exists('zoomcontrol', $atts) ? $zoomcontrol : $settings->get('show_zoom_controls');
$atts['min_zoom'] = array_key_exists('min_zoom', $atts) ? $min_zoom : $settings->get('default_min_zoom');
$atts['max_zoom'] = empty($max_zoom) ? $settings->get('default_max_zoom') : $max_zoom;
$atts['scrollwheel'] = empty($scrollwheel) ? $settings->get('scroll_wheel_zoom') : $scrollwheel;
$atts['doubleclickzoom'] = empty($doubleclickzoom) ? $settings->get('double_click_zoom') : $doubleclickzoom;
$atts['fit_markers'] = empty($fit_markers) ? $settings->get('fit_markers') : $fit_markers;
$atts['scrollwheel'] = array_key_exists('scrollwheel', $atts) ? $scrollwheel : $settings->get('scroll_wheel_zoom');
$atts['doubleclickzoom'] = array_key_exists('doubleclickzoom', $atts) ? $doubleclickzoom : $settings->get('double_click_zoom');
$atts['fit_markers'] = array_key_exists('fit_markers', $atts) ? $fit_markers : $settings->get('fit_markers');

/* allow percent, but add px for ints */
$atts['height'] .= is_numeric($atts['height']) ? 'px' : '';
$atts['width'] .= is_numeric($atts['width']) ? 'px' : '';

/*
need to allow 0 or empty for removal of attribution
*/
if (!array_key_exists('attribution', $atts)) {
$atts['attribution'] = $settings->get('default_attribution');
}

/* allow a bunch of other options */
// http://leafletjs.com/reference-1.0.3.html#map-closepopuponclick
$more_options = array(
Expand Down Expand Up @@ -107,14 +123,6 @@ protected function getHTML ($atts, $content = null) {
$lat = empty($lat) ? $settings->get('default_lat') : $lat;
$lng = empty($lng) ? $settings->get('default_lng') : $lng;

/*
need to allow 0 or empty for removal of attribution
*/
if (!$atts ||
!array_key_exists('attribution', (array) $atts)) {
$attribution = $settings->get('default_attribution');
}

/*
mapquest doesn't need tile urls
*/
Expand Down
2 changes: 1 addition & 1 deletion templates/shortcode-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
'[leaflet-geojson src=https://cdn.rawgit.com/bozdoz/064a7101b95a324e8852fe9381ab9a18/raw/03f4f54b13a3a7e256732760a8b679818d9d36fc/map.geojson fitbounds=1 popup_property="popup-text"]'
),
"Test Image Map" => array(
'[leaflet-image min_zoom=-2 max_zoom=15 zoom=1 zoomcontrol=1 scrollwheelzoom=1]',
'[leaflet-image zoom=1 zoomcontrol=1 scrollwheelzoom=1 attribution=0]',
'[leaflet-marker]'
)
);
Expand Down

0 comments on commit 5845a93

Please sign in to comment.