2

I am getting an error when using map.addLayer or marker.addTo(map) it gives

t.onAdd is not a function

I believe either I am missing any library or what. please help me out to get rid of this problem. latLongShipArray contains lat/long values

var array = [];  // create an global array

for (var i = 0; i < latLongShipArray.length - 1; i++) {  //latLongShipArray consists lat/long values

    if (shipCoordinateArray[i]["Type"][0] == 1) { // condition to add marker ,shipCoordinateArray have some conditions
        marker = new L.marker(latLongShipArray[i], { //add lat/long in new marker
            icon: shipIConBigger
        });
        //marker.addTo(map);
        //map.addLayer(marker);
        marker.on('click', onMarkerPointClick);
    }
    else {   //condition to add marker
        marker = new L.marker(latLongShipArray[i], { // add lat/long in new marker
            icon: shipIConSmaller
        });
        //marker.addTo(map);
        //map.addLayer(marker);
        marker.on('click', onMarkerPointClick);  //click event
    }
    array.push(marker);   //push marker to array
}
var shipCoordinateMarker = L.layerGroup([array]);  // add a layergroup
//shipCoordinateMarker.addTo(map);
map.addLayer(shipCoordinateMarker);  // add marker array to map
1

You initialize your Layer Group with an array of array of Markers:

var shipCoordinateMarker = L.layerGroup([array]);  // add a layergroup

Should be simply:

var shipCoordinateMarker = L.layerGroup(array);  // add a layergroup

since array is already an Array of Markers.

Furthermore, the error you report is probably when using Leaflet version < 1.0.0. When using Leaflet version 1.0.0+, the exact error message due to this mistake should be different. Therefore you should probably upgrade your version.

0

The command for adding a feature/layer group to the map is group.addTo(map);

In your case:

shipCoordinateMarker.addTo(map);
  • still it shows same error – Ankit Kumar Oct 24 '17 at 10:38
  • 1
    Try to remove the new when creating the marker. – BritishSteel Oct 24 '17 at 10:51
  • actually, i have around 30k markers and I want to add it on a map but when I am adding it like that the page gets unresponsive because it takes time. – Ankit Kumar Oct 24 '17 at 12:59
  • That is quite a lot for Leaflet to handle, indeed... you could look into clustering. Maybe you can try it with just a few markers to see if the issue is related to the number of markers (most likely not) or something else. – BritishSteel Oct 24 '17 at 13:37
  • yeah i had tried with least number of markers it works fine but if it goes beyond around 10k then pages gets unresponsive – Ankit Kumar Oct 24 '17 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.