forked from Snck3rs/contao-leaflet-maps
Implement boundsMode fit.
This commit is contained in:
@@ -119,13 +119,25 @@ L.Contao = L.Class.extend({
|
||||
* @param map Pass a map object so that the data loading events are passed to the map.
|
||||
*/
|
||||
load: function (hash, type, options, customLayer, map) {
|
||||
var url = this.createRequestUrl(hash),
|
||||
var url = this.createRequestUrl(hash, map),
|
||||
layer = omnivore[type](url, options, customLayer);
|
||||
|
||||
if (map) {
|
||||
// Required because Control.Loading tries to get _leafet_id which is created here.
|
||||
L.stamp(layer);
|
||||
|
||||
// Add listender for map bounds changes.
|
||||
if (map.options.dynamicLoad && layer.options.boundsMode == 'fit') {
|
||||
layer.options.requestHash = hash;
|
||||
map.on('moveend', layer.refreshData, layer);
|
||||
|
||||
map.on('layerremove', function(e) {
|
||||
if (e.layer === layer) {
|
||||
map.off('moveend', layer.updateBounds, layer);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
map.fire('dataloading', {layer: layer});
|
||||
|
||||
layer.on('ready', function () {
|
||||
@@ -150,7 +162,7 @@ L.Contao = L.Class.extend({
|
||||
* @returns {L.Marker}|{*}
|
||||
*/
|
||||
pointToLayer: function (feature, latlng) {
|
||||
var type = 'marker';
|
||||
var type = 'marker';
|
||||
var marker = null;
|
||||
|
||||
if (feature.properties) {
|
||||
@@ -238,18 +250,20 @@ L.Contao = L.Class.extend({
|
||||
/**
|
||||
* Create request url by appending the hash to the current url.
|
||||
*
|
||||
* @param {string} value The hash
|
||||
* @param {string} value The hash.
|
||||
* @param {L.Map} map The map.
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
createRequestUrl: function (value) {
|
||||
createRequestUrl: function (value, map) {
|
||||
var bounds,
|
||||
key = 'leaflet',
|
||||
params = document.location.search.substr(1).split('&');
|
||||
|
||||
value = encodeURIComponent(value);
|
||||
|
||||
var key = 'leaflet';
|
||||
var params = document.location.search.substr(1).split('&');
|
||||
|
||||
if (params == '') {
|
||||
return document.location.pathname + '?' + [key, value].join('=');
|
||||
value = document.location.pathname + '?' + [key, value].join('=');
|
||||
} else {
|
||||
var i = params.length;
|
||||
var x;
|
||||
@@ -267,8 +281,19 @@ L.Contao = L.Class.extend({
|
||||
params[params.length] = [key, value].join('=');
|
||||
}
|
||||
|
||||
return document.location.pathname + params.join('&');
|
||||
value = document.location.pathname + params.join('&');
|
||||
}
|
||||
|
||||
if (map) {
|
||||
if (map.options.dynamicLoad) {
|
||||
bounds = map.getBounds();
|
||||
value += '&f=bbox&v=';
|
||||
value += bounds.getSouth() + ',' + bounds.getWest();
|
||||
value += ',' + bounds.getNorth() + ',' + bounds.getEast();
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
34
assets/maps/src/Mixin.GeoJSON.js
Normal file
34
assets/maps/src/Mixin.GeoJSON.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Add update bounds method for geo json layers. It is triggered when map bounds changed and make a new request
|
||||
* to get the data in the new bounds.
|
||||
*/
|
||||
L.GeoJSON.include({
|
||||
/**
|
||||
* Update bounds.
|
||||
*
|
||||
* @param {L.Event} e The subscribed event.
|
||||
*/
|
||||
refreshData: function(e) {
|
||||
var dataLayer = L.geoJson(),
|
||||
layer = this;
|
||||
|
||||
dataLayer.on('ready', function() {
|
||||
var i, layers = layer.getLayers();
|
||||
|
||||
// Clear old data.
|
||||
for (i = 0; i < layers.length; i++) {
|
||||
layer.removeLayer(layers[i]);
|
||||
}
|
||||
|
||||
// Copy data from temporary layer.
|
||||
layers = this.getLayers();
|
||||
for (i = 0; i < layers.length; i++) {
|
||||
this.removeLayer(layers[i]);
|
||||
layer.addLayer(layers[i]);
|
||||
}
|
||||
});
|
||||
|
||||
// TODO: Allow other data formats.
|
||||
omnivore.geojson(L.contao.createRequestUrl(this.options.requestHash, e.target), null, dataLayer);
|
||||
}
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
/**
|
||||
* Extend map so that it can calculate their bounds depending of the features with the property affectBounds.
|
||||
* Extend map so that it can calculate their bounds depending of the features with the property boundsMode.
|
||||
*/
|
||||
L.Map.include({
|
||||
_dynamicBounds: null,
|
||||
@@ -63,7 +63,8 @@ L.Map.include({
|
||||
this._dynamicBounds = L.latLngBounds(source, source);
|
||||
}
|
||||
}
|
||||
} else if (L.MarkerClusterGroup && layer instanceof L.MarkerClusterGroup && layer.options.affectBounds) {
|
||||
} else if (L.MarkerClusterGroup && layer instanceof L.MarkerClusterGroup
|
||||
&& layer.options.boundsMode && layer.options.boundsMode == 'extend') {
|
||||
source = layer.getBounds();
|
||||
|
||||
if (source.isValid()) {
|
||||
@@ -73,7 +74,8 @@ L.Map.include({
|
||||
this._dynamicBounds = L.latLngBounds(source.getSouthWest(), source.getNorthEast());
|
||||
}
|
||||
}
|
||||
} else if ((!layer.options || (layer.options && layer.options.affectBounds)) && layer.eachLayer) {
|
||||
} else if ((!layer.options || (layer.options
|
||||
&& layer.options.boundsMode && layer.options.boundsMode == 'extend')) && layer.eachLayer) {
|
||||
layer.eachLayer(this._scanForBounds, this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user