简体   繁体   English

Leaflet JS教程之后的“ this.update不是函数”

[英]“this.update is not a function” following Leaflet JS tutorial

I am copying this tutorial on the Leaflet JS website: https://leafletjs.com/examples/choropleth/ 我正在本Leaflet JS网站上复制此教程: https : //leafletjs.com/examples/choropleth/

The code for adding control is this: 添加控件的代码是这样的:

var info = L.control();

info.onAdd = function(mymap) {
//Create a div with a class of info
      this._div = L.DomUtil.create('div', 'info');
      this.update();
      return this._div;
};

But when I do this, I get the following error: 但是,当我这样做时,出现以下错误:
Uncaught TypeError: this.update is not a function. 未捕获的TypeError:this.update不是函数。

Here is how I incorporated that code from the tutorial into my code: 这是我将教程中的代码合并到我的代码中的方法:

var mymap = L.map('mapid').setView([41.850033, -87.6500523], 3.2);

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
            attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
            maxZoom: 15,
            id: 'mapbox.mapbox-streets-v7',
            accessToken: 'pk.eyJ1IjoiYWxsYW5yaW1iYW4iLCJhIjoiZWEyYTc5NDhmZjdmYjQzM2I3MDdkN2I4OTQ3MjQyNWEifQ.U2GPM8lv7x--qHhbjNo5Ug'
        }).addTo(mymap);

        //control that shows county info
        var info = L.control();

        info.onAdd = function (mymap) {
            //Create a div with a class of info
            this._div = L.DomUtil.create('div', 'info');
            this.update();
            return this._div;
        };

        //method that will update the control based on feature properties passed

        info.udpate = function (props) {
            this._div.innerHTML = '<h4>US Election Result</h4>' + (props ? '<b>' + props.NAME + '</b>' : 'Hover over a county');
        };

        info.addTo(mymap);  

You can see my webpage and the error in the source code here: https://sushirittoman.github.io/maps/counties_project/index.html 您可以在此处查看我的网页和源代码中的错误: https : //sushirittoman.github.io/maps/counties_project/index.html

Please help. 请帮忙。 Thank you. 谢谢。

I don't even know what it is, but I figured out. 我什至不知道它是什么,但我知道了。
The tutorial page also show the final result , which is using exactly the same code. 教程页面还显示最终结果 ,它使用完全相同的代码。
And I started debugging, and got it. 然后我开始调试,并得到了它。
You simply misspelt the word... 你只是拼错了这个词...

info.udpate → info.update

For your information.(just in case) 供您参考(以防万一)
In this case, 'this' means 'info'. 在这种情况下,“ this”表示“ info”。
So you just called own function in that part.(the name update is not important.) 因此,您只是在该部分中调用了自己的函数。(名称更新并不重要。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM