Implement as dynamic setting of the map bounds.

This commit is contained in:
David Molineus
2015-01-20 09:38:35 +01:00
parent 41eb0f7834
commit e83e2c5482
10 changed files with 148 additions and 5 deletions

View File

@@ -0,0 +1,69 @@
/**
* Extend map so that it can calculate their bounds depending of the features with the property affectBounds.
*/
L.Map.include({
_dynamicBounds: null,
/**
* Calculate feature bounds.
*
* Use this method without any arguments to scan the whole map for features.
*
* If a layer is passed then only this layer is used if the map option adjustBounds is set or the force flag
* is given.
*
* @param {L.Layer} layer Optional limit to a layer.
* @param {bool} force Force scaning of a layer no matter if adjustBounds is set.
*
* @return void
*/
calculateFeatureBounds: function(layer, force) {
if (layer) {
if (!this.options.adjustBounds && !force) {
return;
}
this._scanForBounds(layer);
} else {
this.eachLayer(this._scanForBounds, this);
}
if (this._dynamicBounds) {
this.fitBounds(this._dynamicBounds);
}
},
/**
* Scan recursively for bounds in a layer and extend _dynamicBounds if any found.
*
* @param {L.Layer} layer The layer
* @private
*/
_scanForBounds: function(layer) {
var source;
if (layer.feature && layer.feature.properties && layer.feature.properties.affectBounds) {
if (layer.getBounds) {
source = layer.getBounds();
if (source.isValid()) {
if (this._dynamicBounds) {
this._dynamicBounds.extend(source);
} else {
this._dynamicBounds = L.latLngBounds(source.getSouthWest(), source.getNorthEast());
}
}
} else if (layer.getLatLng) {
source = layer.getLatLng();
if (this._dynamicBounds) {
this._dynamicBounds.extend(source);
} else {
this._dynamicBounds = L.latLngBounds(source, source);
}
}
} else if (layer.eachLayer) {
layer.eachLayer(this._scanForBounds, this);
}
}
});

View File

@@ -91,7 +91,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'metapalettes' => array( 'metapalettes' => array(
'default' => array( 'default' => array(
'title' => array('title', 'alias'), 'title' => array('title', 'alias'),
'zoom' => array('center', 'zoom', 'adjustZoomExtra'), 'zoom' => array('center', 'zoom', 'adjustZoomExtra', 'adjustBounds'),
'controls' => array('zoomControl', 'controls'), 'controls' => array('zoomControl', 'controls'),
'layers' => array('layers'), 'layers' => array('layers'),
'interaction' => array( 'interaction' => array(
@@ -370,5 +370,16 @@ $GLOBALS['TL_DCA']['tl_leaflet_map'] = array
'eval' => array('tl_class' => 'clr lng', 'allowHtml'=>true, 'style' => 'min-height: 40px;'), 'eval' => array('tl_class' => 'clr lng', 'allowHtml'=>true, 'style' => 'min-height: 40px;'),
'sql' => "char(1) NOT NULL default ''" 'sql' => "char(1) NOT NULL default ''"
), ),
'adjustBounds' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['adjustBounds'],
'exclude' => true,
'inputType' => 'checkbox',
'default' => false,
'options' => array('load', 'deferred'),
'reference' => &$GLOBALS['TL_LANG']['tl_leaflet_map']['adjustBoundsOptions'],
'eval' => array('tl_class' => 'w50', 'multiple' => true, 'helpwizard' => true),
'sql' => "varchar(255) NOT NULL default ''"
),
), ),
); );

View File

@@ -116,7 +116,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
'riseOffset', 'riseOffset',
'customIcon', 'customIcon',
), ),
'active' => array('active') 'active' => array('active', 'affectBounds')
), ),
), ),
'metasubpalettes' => array( 'metasubpalettes' => array(
@@ -286,5 +286,14 @@ $GLOBALS['TL_DCA']['tl_leaflet_marker'] = array
'eval' => array('maxlength' => 5, 'rgxp' => 'digit', 'tl_class' => 'clr w50', 'nullIfEmpty' => true), 'eval' => array('maxlength' => 5, 'rgxp' => 'digit', 'tl_class' => 'clr w50', 'nullIfEmpty' => true),
'sql' => "int(5) NULL" 'sql' => "int(5) NULL"
), ),
'affectBounds' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_marker']['affectBounds'],
'exclude' => true,
'inputType' => 'checkbox',
'default' => false,
'eval' => array('tl_class' => 'w50'),
'sql' => "char(1) NOT NULL default ''"
),
), ),
); );

View File

@@ -119,7 +119,7 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array
'data' => array(), 'data' => array(),
'popup' => array(':hide','addPopup'), 'popup' => array(':hide','addPopup'),
'config' => array(':hide', 'style', 'className', 'clickable'), 'config' => array(':hide', 'style', 'className', 'clickable'),
'active' => array('active') 'active' => array('active', 'affectBounds')
), ),
'polyline extends default' => array( 'polyline extends default' => array(
@@ -348,5 +348,14 @@ $GLOBALS['TL_DCA']['tl_leaflet_vector'] = array
), ),
'sql' => "mediumblob NULL" 'sql' => "mediumblob NULL"
), ),
'affectBounds' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_leaflet_vector']['affectBounds'],
'exclude' => true,
'inputType' => 'checkbox',
'default' => false,
'eval' => array('tl_class' => 'w50'),
'sql' => "char(1) NOT NULL default ''"
),
), ),
); );

View File

@@ -64,6 +64,13 @@ $GLOBALS['TL_LANG']['tl_leaflet_map']['adjustZoomExtra'][0] = 'Adjust extra z
$GLOBALS['TL_LANG']['tl_leaflet_map']['adjustZoomExtra'][1] = 'Enable if you want to adjust minimum and maximum zoom as well'; $GLOBALS['TL_LANG']['tl_leaflet_map']['adjustZoomExtra'][1] = 'Enable if you want to adjust minimum and maximum zoom as well';
$GLOBALS['TL_LANG']['tl_leaflet_map']['layers'][0] = 'Default layers'; $GLOBALS['TL_LANG']['tl_leaflet_map']['layers'][0] = 'Default layers';
$GLOBALS['TL_LANG']['tl_leaflet_map']['layers'][1] = 'Inital visible layers of the map. For optional layers use the layers control.'; $GLOBALS['TL_LANG']['tl_leaflet_map']['layers'][1] = 'Inital visible layers of the map. For optional layers use the layers control.';
$GLOBALS['TL_LANG']['tl_leaflet_map']['adjustBounds'][0] = 'Adjust bounds';
$GLOBALS['TL_LANG']['tl_leaflet_map']['adjustBounds'][1] = 'If enabled the map will fit to bounds to objects being marked to affect the bounds.';
$GLOBALS['TL_LANG']['tl_leaflet_map']['adjustBoundsOptions']['load'][0] = 'At map initialization';
$GLOBALS['TL_LANG']['tl_leaflet_map']['adjustBoundsOptions']['load'][1] = 'Calculate bounds when the map is initialized. All static and already loaded deferred featured are included.';
$GLOBALS['TL_LANG']['tl_leaflet_map']['adjustBoundsOptions']['deferred'][0] = 'After loading deferred feature';
$GLOBALS['TL_LANG']['tl_leaflet_map']['adjustBoundsOptions']['deferred'][1] = 'Recalculate bounds when a deferred feature is load.';
$GLOBALS['TL_LANG']['tl_leaflet_map']['zoomValues'][''][0] = 'Disable'; $GLOBALS['TL_LANG']['tl_leaflet_map']['zoomValues'][''][0] = 'Disable';
$GLOBALS['TL_LANG']['tl_leaflet_map']['zoomValues'][''][1] = 'Disable zoom function.'; $GLOBALS['TL_LANG']['tl_leaflet_map']['zoomValues'][''][1] = 'Disable zoom function.';

View File

@@ -48,3 +48,5 @@ $GLOBALS['TL_LANG']['tl_leaflet_marker']['icon'][0] = 'Icon';
$GLOBALS['TL_LANG']['tl_leaflet_marker']['icon'][1] = 'Select a custom icon.'; $GLOBALS['TL_LANG']['tl_leaflet_marker']['icon'][1] = 'Select a custom icon.';
$GLOBALS['TL_LANG']['tl_leaflet_marker']['active'][0] = 'Activate marker'; $GLOBALS['TL_LANG']['tl_leaflet_marker']['active'][0] = 'Activate marker';
$GLOBALS['TL_LANG']['tl_leaflet_marker']['active'][1] = 'Only activated markers are rendered on the map.'; $GLOBALS['TL_LANG']['tl_leaflet_marker']['active'][1] = 'Only activated markers are rendered on the map.';
$GLOBALS['TL_LANG']['tl_leaflet_marker']['affectBounds'][0] = 'Affect map bounds';
$GLOBALS['TL_LANG']['tl_leaflet_marker']['affectBounds'][1] = 'If the map support it the marker will be used to affect the initial map bounds.';

View File

@@ -53,3 +53,5 @@ $GLOBALS['TL_LANG']['tl_leaflet_vector']['bounds'][0] = 'Bounds';
$GLOBALS['TL_LANG']['tl_leaflet_vector']['bounds'][1] = 'Each field defines a corner of the bounds as comma separated value (Latitude, longitude [, altitude]).'; $GLOBALS['TL_LANG']['tl_leaflet_vector']['bounds'][1] = 'Each field defines a corner of the bounds as comma separated value (Latitude, longitude [, altitude]).';
$GLOBALS['TL_LANG']['tl_leaflet_vector']['style'][0] = 'Style'; $GLOBALS['TL_LANG']['tl_leaflet_vector']['style'][0] = 'Style';
$GLOBALS['TL_LANG']['tl_leaflet_vector']['style'][1] = 'Choose a style. If none defined, the default style of leaflet is used.'; $GLOBALS['TL_LANG']['tl_leaflet_vector']['style'][1] = 'Choose a style. If none defined, the default style of leaflet is used.';
$GLOBALS['TL_LANG']['tl_leaflet_vector']['affectBounds'][0] = 'Affect map bounds';
$GLOBALS['TL_LANG']['tl_leaflet_vector']['affectBounds'][1] = 'If the map support it the vector will be used to affect the initial map bounds.';

View File

@@ -96,7 +96,10 @@ class MarkersLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
$marker = $mapper->handle($item); $marker = $mapper->handle($item);
if ($marker instanceof Marker) { if ($marker instanceof Marker) {
$definition->addData($marker->toGeoJsonFeature(), true); $feature = $marker->toGeoJsonFeature();
$feature->setProperty('affectBounds', ($item->affectBounds));
$definition->addData($feature, true);
} }
} }
} }

View File

@@ -18,6 +18,7 @@ use Netzmacht\Contao\Leaflet\Frontend\RequestUrl;
use Netzmacht\JavascriptBuilder\Type\Expression; use Netzmacht\JavascriptBuilder\Type\Expression;
use Netzmacht\LeafletPHP\Definition; use Netzmacht\LeafletPHP\Definition;
use Netzmacht\LeafletPHP\Definition\GeoJson\ConvertsToGeoJsonFeature; use Netzmacht\LeafletPHP\Definition\GeoJson\ConvertsToGeoJsonFeature;
use Netzmacht\LeafletPHP\Definition\GeoJson\Feature;
use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection; use Netzmacht\LeafletPHP\Definition\GeoJson\FeatureCollection;
use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonFeature; use Netzmacht\LeafletPHP\Definition\GeoJson\GeoJsonFeature;
use Netzmacht\LeafletPHP\Definition\Group\GeoJson; use Netzmacht\LeafletPHP\Definition\Group\GeoJson;
@@ -109,7 +110,13 @@ class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
$vector = $mapper->handle($item); $vector = $mapper->handle($item);
if ($vector instanceof ConvertsToGeoJsonFeature) { if ($vector instanceof ConvertsToGeoJsonFeature) {
$definition->addData($vector->toGeoJsonFeature(), true); $feature = $vector->toGeoJsonFeature();
if ($feature instanceof Feature) {
$feature->setProperty('affectBounds', (bool) $item->affectBounds);
}
$definition->addData($feature, true);
} }
} }
} }
@@ -141,6 +148,10 @@ class VectorsLayerMapper extends AbstractLayerMapper implements GeoJsonMapper
} }
if ($vector instanceof GeoJsonFeature) { if ($vector instanceof GeoJsonFeature) {
if ($vector instanceof Feature) {
$vector->setProperty('affectBounds', (bool) $item->affectBounds);
}
$feature->addFeature($vector); $feature->addFeature($vector);
} }
} }

View File

@@ -63,6 +63,7 @@ class MapMapper extends AbstractMapper
$this->buildCustomOptions($map, $model); $this->buildCustomOptions($map, $model);
$this->buildControls($map, $model, $builder, $bounds); $this->buildControls($map, $model, $builder, $bounds);
$this->buildLayers($map, $model, $builder, $bounds); $this->buildLayers($map, $model, $builder, $bounds);
$this->buildBoundsCalculation($map, $model);
} }
} }
@@ -150,4 +151,23 @@ class MapMapper extends AbstractMapper
} }
} }
} }
/**
* Build map bounds calculations.
*
* @param Map $map The map being built.
* @param MapModel $model The map model.
*/
private function buildBoundsCalculation(Map $map, MapModel $model)
{
$adjustBounds = deserialize($model->adjustBounds, true);
if (in_array('deferred', $adjustBounds)) {
$map->setOption('adjustBounds', true);
}
if (in_array('load', $adjustBounds)) {
$map->calculateFeatureBounds();
}
}
} }