Google Maps V3 Cluster will not reset after clearMarkers() - javascript

I want to add and remove data sets from the clusterer - but when a particular data set cluster is toggled off the original (non-clustered) markers dont re-appear - i cant make them visible...
function clusterEvents(i)
{
cluster[0][i]=cluster[0][i]?0:1 // to tell which data set is in use
if(cluster[1]) // clear the cluster
cluster[1].clearMarkers()
cluster[1]=new MarkerClusterer(map,[],{gridSize:50,maxZoom:15})
for(i in iconz) // import markers
{
if(iconz[i].type<20)
{
if(cluster[0][iconz[i].type]||cluster[0][iconz[i].type-6]) // if target then cluster
cluster[1].addMarker(iconz[i],true)
else
**iconz[i].setVisible(true) // make it visible**
}
}
}

The problem lay with the syntax of making the markers visible in the code above
iconz[i].setVisible(true)
the correct syntax is:
iconz[i].setOptions({map:map[j_W],visible:true})
whats nice about this is that clusters can be assigned to their own layers/arrays and can be toggled on and off

Related

Leaflet - markers duplicating, removeLayer, clearLayers only hide markers

markerGroup: L.LayerGroup;
this.markerGroup.eachLayer(function (layer) {
this.markerGroup.removeLayer(layer);
});
this.markerGroup.clearLayers();
removeLayer() and clearLayers() are working as described in the leaflet documentation but I am wondering if there is a way to remove the layer/marker/instance from my markerGroup variable entirely. Even though the markers are cleared/removed, when my refresh button is called it still has the previous markers in the markerGroup variable and adds the same ones again thus duplicating them. This is a problem as I have shadows for these markers which duplicate over each other and eventually becoming 100% opaque. Has anybody got any suggestions or come across this problem before?
Edit: Thanks for the suggestion #Falke Design. map.removeLayer doesn't work for me unfortunately. Hopefully the information below is more helpful:
refreshButton() {
this.markerGroup = new L.LayerGroup;
clearInterval(this.refreshTimer);
this.dynamicLatLong = this.map.getCenter();
this.dynamicZoom = this.map.getZoom();
this.markerGroup.clearLayers();
this.polygonGroup.clearLayers();
this.map.removeLayer(this.markerGroup);
this.getCurrentAzure();
this.leafletMap();
}
getCurrentAzure() uses a for loop to get the data such as lat, lng, etc. of each instance and assigns it to a variable named marker. Each marker is added to markerGroup with this line:
marker.addTo(this.markerGroup);
leafletMap() creates the map and draws the markerGroup onto the map with this line:
this.markerGroup = L.layerGroup().addTo(this.map);

How to add markers to leaflet map with tabletop.js?

I'm using this quite nice guide to add markers from a Google sheet to a basic leaflet.js map:
https://rdrn.me/leaflet-maps-google-sheets/
The problem is, using these code snippets here i get all the data logged and returned in the console, but none of the points appear on the map itself.
This is probably some really basic JavaScript issue that i'm not able to see. Sorry, still learning.
Here's a jfiddle, linking to a demo sheets with one marker point
https://jsfiddle.net/xfs19cz7/1/
with the map part:
function init() {
Tabletop.init({
key: '*url to gsheets here*',
callback: myFunction,
simpleSheet: true
})
}
window.addEventListener('DOMContentLoaded', init)
function myFunction(data, tabletop) {
console.log(data);
}
var map = L.map('map-div').setView([64.6220498,25.5689638], 5);
var basemap = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Basemap (c) OpenStreetMap',
minZoom: 5,
maxZoom: 18
});
basemap.addTo(map);
function addPoints(data, tabletop) {
for (var row in data) {
var marker = L.marker([
data[row].Latitude,
data[row].Longitude
]).addTo(map);
marker.bindPopup('<strong>' + data[row].Info + '</strong>');
}
}
This should add one point to a basic map. Now actually the map is not at all rendered, and no marker shows up. I can't find any issues in the code making the map show up blank, but there probably are some.
The marker from gsheets is however logged in the console, i suspect there is something missing in my code relating to really basic javascript functions / looping / sloppy syntax.
Also tried the init() and addPoints(data, tabletop) parts to a map i had where the map with the same basemap link, which rendereded OK. Adding this still left the map rendering, but no markers showed up. Again, the gsheets was loaded as an array of objects.
Could anyone point me to this, probably very basic, issue in the code?
edit:
callback: myFunction,
line above needs to be changed to
callback: addPoints,
also, init function needs to be called and position set to absolute. Thanks for the working fiddle in answer marked as correct below.
Fixes
Try setting map position absolute
calling the init() function
Working fiddle

why map.fitBounds(group.getBounds()) works with one set of data and not with another set of data?

I try to see all the markers on a map.
To that end I implement the following logic
All logic is inside an object.
setLocation(places) {
try {
const {
markers,
markerArray
} = this.getMarkers(places);
const group = L.featureGroup(markerArray);
this.map.addLayer(markers);
this.map.fitBounds(group.getBounds());
} catch (error) {
console.log(`Error: ${error.message} Probably no places found`);
}
},
places is an array of objects, where each object includes the properties latitude and longitude. For each of these objects a marker is created. The logic is as follows and is invoked from the getMarkers() method
getMarkers(points = buysmoke.points) {
const markers = L.markerClusterGroup();
const markerArray = [];
for (const point of points) {
const content = this.getContent(point);
const icon = this.createIcon(point.categoria);
const popup = this.createPopup(content);
const marker = L.marker([point.latitud, point.longitud], {
icon: icon
}).bindPopup(popup);
markerArray.push(marker);
markers.addLayer(marker);
}
return {
markers: markers,
markerArray: markerArray
};
},
This logic is working with a set of data, which correspond to America. When I try the same with a data set from Europe the logic does not work and instead shows me the map of the whole world. The rest of the interactions on the map continue to work.
I have tried to understand the reason for this, but without success.
If you need any other information, please let me know.
Thank you
Update 1
The data set of Europe consists of 195 records, probe only process 12 of them and works as expected. Is it somehow possible that some latitude and longitude is proving the problem? If it were a possibility is there some friendly way to track the problem?
Update 2
Indeed, the problem is caused due to a series of wrong lengths, a special one whose coordinate does not exist. When removing it from the data set, the markers are drawn on the map as estimated
In any case, thank you very much. I hope this experience serves someone

Leaflet MarkerCluster open/spiderfy cluster based on known marker

My scenario is that the user would click on a marker that is part of a cluster and be redirected somewhere else. Then when they come back they need to return the the same bounds on the map where they were before at that marker, but the cluster in which the marker is found is no longer expanded/spiderfy'd, which I need to get done.
At this point I know which marker I'm looking for, but need to expand its cluster. What I've done so far:
Iterate through the markers in the initially populated L.markerClusterGroup() object:
layers = L.markerClusterGroup();
. . . populate 'layers' ...
$.each(layers, function (idx, layer) {
if(layer._tooltip._content === 'known marker tooltip') {
layer.__parent.spiderfy();
}
});
Although the spiderfy() function sort of works, it doesn't seem to be intended to be used on its own and breaks the cluster pretty bad.
Alternatively, I've tried calling fire('clusterclick') on the above layer object, as well as on layer.__parent, which I presume would represent the cluster, but can't get anything working.
I would need a solution in which I can properly trigger the clusterclick event that would handle everything, as if I had actually clicked the cluster myself.

How can I list Mapbox gridLayer elements that are in the user view?

I'd like to list all of the elements from a Mapbox gridLayer that are visible to a user in the viewport.
There is an excellent example on the Mapbox site called 'Listing markers in view'. It shows all the markers on the featureLayer that are in the view of the browser.
I'd like to create something similar, but using using a gridLayer instead. (the gridLayer was created in TileMill)
Here is an example fiddle of the data with a version of non-working 'in-bounds' code:
http://jsfiddle.net/bfab/uSLVw/1/
For reference, the gridLayer has a variable in it (passed from TileMill in the UTFGrid) called '{{{Magnitude}}}' I'd like to list each of the instances of earthquakes that appear to the user, and add it to a list on the bottom left of the example. The function I'm trying to use is gridLayer.getData(latlng,callback).
Here is the snippet of code that is not working:
map.on('move', function() {
// Construct an empty list to fill with gridLayer elements.
var inBounds = [],
// Get the map bounds - the top-left and bottom-right locations.
bounds = map.getBounds();
// For each part of the grid, consider whether
// it is currently visible by comparing with the current map bounds.
// This is what fails....
myGridLayer.getData(bounds,function(earthquake){
inBounds.push(earthquake.Magnitude);
});
// Display a list of markers.
document.getElementById('coordinates').innerHTML = inBounds.join('\n');
});
Apologies if I am making a simple error...
Actually you can't (not easily at least)
1/ You misunderstand the getData function on the gridlayer
first parm is not a boundary but a location latlng
when you use this in a callback (onclick for example) you will get data only if you click on one of your circles, if not you get undefined). The data is not a list of markers but the information of your earthquake (e.g. {DateTime: "2014-04-26T16:59:15.710+00:00", Magnitude: 3.9})
myGridLayer.getData(event.latlng, function(data) {
console.log(data);
// if clicked on a marker: data is defined
// e.g. Object {DateTime: "2014-04-26T16:59:15.710+00:00", Magnitude: 3.9}
});
http://jsfiddle.net/FranceImage/uSLVw/11/
2/ It is not easy to think in terms of markers when using gridlayer as the markers are already part of the image when you load tiles:
http://a.tiles.mapbox.com/v3/bfab.i4nm0adm/3/0/2.png
The interaction is described in some javascript loaded at the same time
http://a.tiles.mapbox.com/v3/bfab.i4nm0adm/3/0/2.grid.json

Categories

Resources