setBounds to two different featureGroups at once mapbox leaflet? - leaflet

I am working on a map where I have two feature layers.
var myFeatureGroup1 = L.featureGroup().addTo(map);
var myFeatureGroup2 = L.featureGroup().addTo(map);
I am setting bounds like:
map.fitBounds(myFeatureGroup1.getBounds());
map.fitBounds(myFeatureGroup2.getBounds());
But for obvious reasons, myFeatureGroup2 is set bounds on. Is ther a way by which I can fit bounds to multiple layerGroups? Like both of them at once? Is there a way I can merge them into a third layerGroup and fit bounds on it?

map.fitBounds(myFeatureGroup1.getBounds().extend(myFeatureGroup2.getBounds()));
See the LatLngBounds documentation, this is the first documented method.

How about using TurfJS to merge the two bounds together(convert them to geoJSON) first, then map.fitBounds(result).

Related

Is there a way to combine two raster mapbox layears

I want to implement something similar to mapbox-compare feature. But instead of having side by side sync maps I need to show one layer over another but only inside some area(s) on map e.g.
Lets say i want to combine default lite map style with some third-party raster source
Something like this https://jsfiddle.net/e9wqdfgn/11/
function getD3() {
var bbox = document.body.getBoundingClientRect();
var center = map.getCenter();
var zoom = map.getZoom();
// 512 is hardcoded tile size, might need to be 256 or changed to suit your map config
var scale = (512) * 0.5 / Math.PI * Math.pow(2, zoom);
var d3projection = d3.geo.mercator()
.center([center.lng, center.lat])
.translate([bbox.width/2, bbox.height/2])
.scale(scale);
return d3projection;
}
but without d3.js
Is it possible? Thanks in advance!
You could probably do it like this:
Position two maps directly above each other, same dimensions.
Use mapbox-gl-sync-move to keep their positions in sync.
Use CSS clipping so that most of the top one is not shown. The code within mapbox-gl-compare might help.
You can simply use mapbox
It has very simple integration, sharing video for your reference https://youtu.be/-VQ4wheGruI

Mapbox Filtering Multipe Tile Layers at Once

Is it possible to set a filter for multiple tile layers at once? I currently have data stored in vector tile and the filters I am applying are near instantaneous - which is awesome!
That said, I would like to filter two different geometry types (line and circle) at once on the map. Each line will be tied to a circle.
Is this possible to do easily? It would be great if anyone knows of an example.
Thanks!!
Using mapbox-gl-js, it's as simple as:
for (const each layer of ['layer1', 'layer2']) {
map.setFilter(layer, myNewFilter)
}
Or with mapbox-gl-utils:
map.U.setFilter(['layer1', 'layer2'], myNewFilter);

Remove points of polylines that are outside of polygon using Leaflet

I've draw polyline programmatically (not using leaflet draw) inside polygone using leaflet draw plugin on map, I want to keep only the points of polyline that are inside of polygone and remove those are outside. Do you have any idea how to do that using a leaflet plugin?. Any help is much appreciated. Thanks
Here is a screenshot:
The expected result:
I did research on difference method of **turf" library as #Sam suggested, so finaly I can apply this method on my drawing polygon and line, here is a code snippet:
var line = path.toGeoJSON();
var polygon = selectedPoly.toGeoJSON();
var difference, result = [];
difference = turf.difference(line, polygon);
if (difference)
{
result.push(difference);
var inter = L.geoJson(result).addTo(map);
}
This is a screenshot of the result:
Now I want to remove this part of line and keep only the section inside polygon, I tried to do that but not working. Can you help me please? Thank you
I'm working with turfjs to check for overlapping polygones in leaflet.
map.on('draw:created', function (e) {
var intersection = [];
otherPolysLayer.eachLayer(function (layer) {
if (!_.isUndefined(turf.intersect(e.layer.toGeoJSON(), ))) {
intersection.push(layer);
}
})
});
You could change the above so that instead it checks for the entire polygone, you'd check with the difference method.
difference: Finds the difference between two polygons by clipping the
second polygon from the first.
I've been looking for a good while for a decent library and looked into leaflet-pip, kevlindev amongst others but I find that turf really just works out of the box.
Update
http://jsfiddle.net/ddce1wh5/ how about this? I used intersect, because that is apparently the part you'd like to keep, I misread, apologies. The following http://jsfiddle.net/mcqL1y90/ we use an array of lines that uses either the intersecting line, or if no intersection is taking place it takes the line itself to draw on the map.

How is the Layer Order is decided in leaflet map?

When I use the following code to go through all the layers in a leaflet map,
map.eachLayer(function (layer) {
console.log(layer);
});
I just wants what rule is used to create this order. Is it because of the ZIndex of the layer? Or is it because the layer is added in different time? If the layer is added early, it will be in the bottom.
Thanks,
Good question, it seems the LeafletJS docs don't say anything about the ordering of the layers via eachLayer, so...
Looking at the code on github, L.Map.eachLayer loops through L.Map._layers which is an object (not an array). So technically the layers could come back in any order...
That said, the individual layers are added to the _layers object in the order they are added to the map. So in all likelihood, eachLayer will be iterating over the layers in that same order (whatever order they were added to the map).

Zoom in on maker group in leaflet mapbox?

I have a layerGroup in my map which has a lot of markers and another layerGroup which has a lot of circles. I need to zoom in on these layer groups but I cannot find the exact API method to do so.
I have tried:
var myLayerGroup = L.layerGroup().addTo(map);
myLayerGroup.fitBounds(); // tried this
myLayerGroup.setBounds(); // and this too
I am not sure which method in the API will work for me. How to zoom in on a layer of markers?
Switch from using layerGroup to featureGroup, theb you can use getBounds() on a feature group to retrieve its bounds, then use map.fitBounds(result) to zoom to that view.