最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Leaflet error: clearLayers is not a function - Stack Overflow

programmeradmin0浏览0评论

I'm using a Leaflet offline map with React where I manually load in a countries.js GeoJSON file with every country in the world. I then conditionally color each country depending on data received from this.props.data, which works initially.

However, the issue es from updating the colors when new this.props.data is ing in.

I have tried managing it in these two ponents:

ponentWillReceiveProps(nextProps) {
    if(this.props.data) {
        let map = this.map;
        console.log(map); // It shows the leaflet object!
        console.log(nextProps.data); // It's logged
        console.log(this.props.data); // Also logged
        if (nextProps.data !== this.props.data) {
            map.clearLayers(); // Error es here?
            console.log("New data doesn't match old!"); // This logs correctly
        }
    }
},

ponentDidUpdate(prevProps, prevState) {
    if(this.props.data){
        let map = this.map;
        if (prevProps.data !== this.props.data) {
            L.geoJson(this.countries, { // from import
            }).addTo(map);
         }
    }
},

What am I doing wrong? The condition is met, if the old data doesn't match the new, the map should clear all layers with clearLayers();. I could understand this error if the map couldn't be found; but I am accessing it with this.map, so it doesn't appear to be a scoping issue.

I'm using a Leaflet offline map with React where I manually load in a countries.js GeoJSON file with every country in the world. I then conditionally color each country depending on data received from this.props.data, which works initially.

However, the issue es from updating the colors when new this.props.data is ing in.

I have tried managing it in these two ponents:

ponentWillReceiveProps(nextProps) {
    if(this.props.data) {
        let map = this.map;
        console.log(map); // It shows the leaflet object!
        console.log(nextProps.data); // It's logged
        console.log(this.props.data); // Also logged
        if (nextProps.data !== this.props.data) {
            map.clearLayers(); // Error es here?
            console.log("New data doesn't match old!"); // This logs correctly
        }
    }
},

ponentDidUpdate(prevProps, prevState) {
    if(this.props.data){
        let map = this.map;
        if (prevProps.data !== this.props.data) {
            L.geoJson(this.countries, { // from import
            }).addTo(map);
         }
    }
},

What am I doing wrong? The condition is met, if the old data doesn't match the new, the map should clear all layers with clearLayers();. I could understand this error if the map couldn't be found; but I am accessing it with this.map, so it doesn't appear to be a scoping issue.

Share Improve this question asked Mar 22, 2017 at 9:49 cbllcbll 7,23929 gold badges79 silver badges124 bronze badges 3
  • 1 r u sure clearLayers() function exist ? check the spelling may be it could be a spelling mistake. – Mayank Shukla Commented Mar 22, 2017 at 10:06
  • 1 clearLayers is method of the LayerGroup not of the map – xmojmr Commented Mar 22, 2017 at 10:35
  • Seems like you're right, I have to objectify the geojson data and run clearLayers it with this to avoid scoping constraints. – cbll Commented Mar 22, 2017 at 10:46
Add a ment  | 

1 Answer 1

Reset to default 6

clearLayers() is a method of L.LayerGroup, not a method of L.Map.

Store the instance of L.GeoJson that you create when running L.geoJson(...), and implement more fine-grained control of when it's added/removed from the map.

发布评论

评论列表(0)

  1. 暂无评论