javascript - Uncaught TypeError: layer.onAdd is not a function - Stack Overflow

First attempt at working with Leaflet to display and work with a dynamic map, and I'm running into

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['pany'] 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 ing back via the variable panies, I get the error:

myMap.clearLayer('listings');
myMap.updateListings(panies, '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: .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.

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['pany'] 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 ing back via the variable panies, I get the error:

myMap.clearLayer('listings');
myMap.updateListings(panies, '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./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.

Share Improve this question edited May 25, 2015 at 22:49 Mike T asked May 25, 2015 at 8:53 Mike TMike T 3597 silver badges18 bronze badges 1
  • An online demo is always the best way to debug... – Jonatas Walker Commented May 25, 2015 at 10:08
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745501815a4630451.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信