I am trying to build an application that uses leaflet, PostgreSQL and PHP. I have created the APIs in PHP and added some layers from PostgreSQL and it works fine. The main theme of the app is to allow the user to be able to filter the points using some fields(its working fine) and I am also trying to show a heatmap that also gets filtered based on the form. I used this heatmap.js (https://github.com/Leaflet/Leaflet.heat) addon for leaflet. Initially all the data loads on my webpage and I am able to add the heat layer too using this code below:
geoJson2heat = function(geojson, weight) {
return geojson.features.map(function(feature) {
return [parseFloat(feature.geometry.coordinates[1]),
parseFloat(feature.geometry.coordinates[0]),
parseFloat((feature.properties[weight]))];
});
}
var geoData = geoJson2heat(lyr, 'area_cos');
var myconf = {
minOpacity: 0.7,
radius: 25,
blur: 25,
max: 1300
};
var heat = L.heatLayer(geoData, myconf).addTo(map);
layerControl.addOverlay(heat, 'Heat Map');
This displays the heatmap perfectly fine. Now I also have a form that allows user to take inputs and filter the data based on that. So when a user submits the form, it gets the filtered data from postgresql and displays it. When I try to apply the same concept to this heat layer, it does not work. I am not able to remove the layer, rather it shows me an error. This is the code I am trying to use:
$('#filterForm').submit(function(e){
e.preventDefault()
map.removeLayer(heat);
layerControl.removeLayer(heat);
.
. rest of the code here working fine
.
});
I get this error:
Uncaught TypeError: Cannot read properties of undefined (reading '_leaflet_id')
I try to print some properties of the heat layer to see if its displaying or not to make sure my layer is correct in the code but get the same error (even thought the layer is perfectly visible on the map and works fine). But if I try to print the properties of this layer outside of this jquery form submit listener, it works and shows me the correct information. I suspect that it has something to do with me calling the remove layer in the jquery listener specifically for the heat layer (because I removed other layers, point layer within this jquery listener and its working fine). I was hoping if you could tell me what the issue is and help me get a work around of this. I am just a beginner, so stuck in here.
Thanks!
Related
I am working on a Leaflet project, which can have quite a few markers on the map. The behavior that I have currently is, if you click on any of the cluster it will spiderify to show all the marker and the user can hover over individual markers to see more information about it. As the number of markers can be somewhere around 100 - 10K it can be a real issue to show all of them.
Theoretically, I would like to show 9 items and 1 items which will be kind of More marker containing information about the rest of the marker, which can be clicked which would lead to a different page.
My current implementation looks like this on clustermouseover.
private createMarkerClusterGroup() {
if (this.markerClusterGroup) { return; }
this.markerClusterGroup = L.markerClusterGroup(this.effectiveMarkerClusterGroupOptions);
this.markerClusterGroup.on('clusterclick', (event: any) => {
const cluster: ExtendedMarkerCluster = event.layer;
event.layer.spiderfy();
});
Few of the things I have been able to figure out are :-
I can get the total children markers using const markersCount = cluster.getAllChildMarkers();
I will have to remove all the extra markers and set the new markers location equal to any one of the removed markers and set a key with all the id's for them to be retrieved afterwards.
I am unable to put them together and make them work. Any help is appreciated.
I had kind of similar problem once. I resorted to showing a popup containing all of the markers rendered in a list as it was more clear to see. From what I recall I also had to render each marker with a data-id attribute, just to be able to get the data needed for tendering the popup.
Let me know if this helps a bit and don't hesitate to ask for more.
I'm trying to use leaflet to make an interactive map (like a choropleth) using data from geojson files and I've a problem to update data and legend on the map, dynamically.
My code use a leaflet map with a mapbox-light baselayer, a dropdownlist and a legend. Two jsons files are loaded and I use two map panes to use them and set their z-index. Dropdownlist has two options and is used to change the selected geojsons to expand this example; the real case has 25 values to choose.
I use $(id_dropdownlist).change to make everything. If ddl change:
remove panes if these exists.
select geojsons files using ddl value (default url myjson files + ajax for this example).
add panes, load and color polygons.
When I make the first change in the ddl works fine: select geojsons files, load and color polygons. My problem appears from the second change onwards, after remove panes: geojsons are loaded successfully but these are not showed/colored on my map.
My amateur debugging detect than in the first change the div leaflet-pane element has the geojsons values, but from the second change onwards the same div leaflet-pane has not data.
Also, I want to update the legend at same time after load the geojsons, but I do not have any idea how to make that.
Here is a functional example in JSFiddle.
If someone could help me step by step, for learn it, I would be very grateful. If panes are not the way to do this, or another way to do this better, please tell me how.
When user changes the selection of your drop down list, you first try to clear your map by removing your previously created custom panes from the DOM.
There is a very high chance Leaflet does not expect such fiddling with its panes, even if they are custom. That is probably why when you create new custom panes with the same names, Leaflet does not reference them correctly, hence it does not display your new layers in those new panes.
For adding and removing your layers dynamically to / from the map, you should simply use Layer Groups instead. In particular, it provides you with a clearLayers method that does what you are trying to achieve.
Therefore, you just add your polygons and markers to that Layer Group (instead of directly to the map), so that you can easily clear them when the user selects a new value. The custom panes remain unaffected.
// Initialize the Layer Group.
// Add it to map so that its children will appear on map.
var layerGroup = L.layerGroup().addTo(map);
// On <select> "change" event:
layerGroup.clearLayers();
// When you have retrieved your data and are ready to convert it to layers:
L.geoJSON(data, {
style: { // Can be a function as usual.
pane: 'polygon'
},
pointToLayer: function(feature, latlng) {
return L.circleMarker(latlng, {
pane: 'POI'
});
}
}).addTo(layerGroup); // Add to layerGroup instead of directly to map.
Updated JSFiddle: https://jsfiddle.net/Ljetvd2x/4/
As for updating your legend, I am sure you should be able to find some resources on that topic, including here on SO. If you still need help, that should very probably be separated into a new post.
I have five animated heat map time series generated in carto. I need to display them one by one when a button is clicked in my webpage.
Here is a screenshot of my page
screenshot
The idea is that only the relevant time series map should display for the button clicked.
I managed to get heat-maps to load on button click with below method, but it doesn't clear the previously active layer and also the time-series control (play/pause) seem to only apply to the initial layer.
var LayerActions = {
button1: function(){
var sublayer = layer.getSubLayer(0);
cartodb.createLayer(map, layerUrl, layerOptions).addTo(map);
return true;
},
button2: function(){
cartodb.createLayer(map, layerUrl2, layerOptions).addTo(map);
return true;
}
Appreciate if someone can point me in the right direction.
Thanks in advance!
yajith
Instead of creating and removing layers, I recommend hiding and showing them as explained in this working example.
First, you have to create a createSelector function which accepts layers as the argument, and activates when clicking. Thus showing and hiding your layers as needed. Secondly, create a visualization or a layer with sublayers and invoke the previous function.
I have a web map with vector points. Once you click one, FramedCloud popup appears with data based on this feature.vector attributes.
There is an option to search through all points by input text field with added jquery autocomplete. However, if user uses this autocomplete, only one vector.point remains on the whole map. I would like to make that this point is automatically clicked.
selector: new OpenLayers.Control.SelectFeature(warstwa_ikon, { onSelect: PopupLokal, onUnselect: destroyPopup }
warstwa_ikon is my vector layer, PopupLokal(feature) is function generating FramedCloud.
I tried this:
map.events.triggerEvent('click', warstwa_ikon.features[0]);
But it gives an error: TypeError: (intermediate value)._featureId is undefined.
I was trying warstwa_ikon.features[0].id and few other methods of giving this function id of my lonely vector point, but with no success.
How can I retrieve and object on google map without user interaction. Meaning the user do not click on the object.
I have 10 polygons and I want user to navigate to each of them, manipulate maybe the color.
without clicking on the polygon itself.
I do have a "next" button that when user click, it suppose to navigate to the next polygon setting it active and to be filled with desire color.
The polygons are store in an array with json format.
Im using Data Layer. I am not sure how to implement this. I can click on polygon itself and change the color, but I need to traverse them without clicking.
Thanks
making map:
function loadJson(url, style)
{
var jsonfile = $.getJSON(url).done(function (data){
map.data.addGeoJson(data);
map.data.setStyle(style);
});
}
change color
map.data.addListener('click', function(event) {
mapName.data.overrideStyle(event.feature, currentStyle);
});
WORKING PROTOTYPE
In a data-layer there is no way to access the shapes(e.g. polygons) that have been drawn, you only may access the features.
The problem: the features are not available as array(then it would be easy to iterate over the features or select a feature by index)
Possible solution:
store the polygon-features in an array when they will be added to the layer(could be done via a addfeature-callback)
calculate the bounds of the polygons:(See: How to get LatLngBounds of feature polygon geometry in google maps v3?)
to implement the "next"-feature:
use map.data.revertStyle() to reset the style of the previous selected feature
select the first feature in the array and apply the desired style and call map.fitBounds() based on the calculated bounds(step2)
put the first feature to the end of the array
arrayWithFeatures.push(arrayWithFeatures.shift());
Demo: http://jsfiddle.net/doktormolle/anmfe/