Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: layer.onAdd is not a function

First attempt at working with Leaflet to display and work with a dynamic map, and I'm running into an error when I attempt to add a new layer of markers to a LayerGroup.

Here is my Map object, encapsulating the functionality of leaflet:

function Map() {
  //properties/fields
  var map;
  var layers = [];  

  return {
    setMap: function(aMap) {map=aMap;},
    setView: function(aView) {map.setView(aView);},
    addLayer: function(aLayer, name) {layers[name] = aLayer; map.addLayer(aLayer);},    
    addListings: function(anArr, name) {
        var mLayer = [];                                           
        for (var i = 0; i < anArr.length; i++) {
            var aMarker = L.marker([anArr[i][0], anArr[i][1]]);
            mLayer.push(aMarker);                            
        };
        layers[name] = L.layerGroup(mLayer);
        layers[name].addTo(map);
    },
    updateListings: function(anArr, name) {
        var mLayer = [];
        for (var i = 0; i < anArr.length; i++) {
            console.log(anArr[i].entity.locations[0].lat, anArr[i].entity.locations[0].lng);
            var aMarker = L.marker(anArr[i].entity.locations[0].lat, anArr[i].entity.locations[0].lng);
            mLayer.push(aMarker);
        }
        layers[name].addLayer(mLayer);
    },
    clearLayer: function(name) { layers[name].clearLayers(); },
  };
}

When I load the map initially, everything's fine:

                myMap.setMap(L.map('tikit-map').setView([{{ $result['lat'] }}, {{ $result['lng'] }}], 12));                        
                var listLocation = [];
                @foreach ($result['company'] as $facility)                        
                    listLocation.push([{{ $facility['entity']['locations'][0]['lat'] }}, {{ $facility['entity']['locations'][0]['lng'] }}]);                            
                @endforeach  
                myMap.addListings(listLocation, 'listings');                      

Then, when I need to refresh the screen (and the map) via an ajax call (with data coming back via the variable companies, I get the error:

myMap.clearLayer('listings');
myMap.updateListings(companies, 'listings');    

The error specifically occurs in the line:

layers[name].addLayer(mLayer);

of updateListings

Anyone with some experience with leaflet that can offer some advice? Thanks

UPDATE: The issue I'm having revolves about why I can't "reuse" the LayerGroup after I've cleared it, and how I would go about doing that. I've struggled through the first half of this day for a solution and was about to post the code as a demo on jsfiddle when I came across this: https://github.com/Leaflet/Leaflet/blob/master/FAQ.md You can add the Google Maps API as a Leaflet layer with a plugin. But note that the map experience will not be perfect, because Leaflet will just act as a proxy to the Google Maps JS engine, so you won't get all the performance and usability benefits of using Leaflet when the Google layer is on.

Because the requirement of the project is to use Google maps, I am abandoning my efforts, perhaps someone needing to do this will benefit from any future answers.

like image 465
Mike T Avatar asked May 16 '26 10:05

Mike T


1 Answers

You are creating a plain vanilla javascript array with var mLayer = [];, when you really need to be using the Leaflet construct for arrays of layers, which is either L.layerGroup or L.featureGroup - depending on the amount of interactivity. It sounds like for your usecase, var mLayer = L.layerGroup would be fine.

like image 172
snkashis Avatar answered May 17 '26 22:05

snkashis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!