Leaflet地图初始化时对 prototype引用造成的影响

今天上午在对Leaflet地图的点添加点击事件的时候发现,Leaflet的初始化可能会对prototype中函数对prototype其他函数引用造成影响,在渲染地图时,一般讲gis 传入对应的渲染方法中。具体参见如下代码:

(function ($) {
    Demo = function(element,options){
        gis = this;
        //renderMap中传入gis对象。前几次不传发现失败,不能调用
        //protorype其他的函数。
        gis.renderMap(gis);
        gis.printPoint();

    }

    Demo.prototype =  {
        property : 'x',
        renderMap : function () {
            var map = gis.map = L.map('mapid',{
                renderer: L.canvas()
            });

            map.setView([30, 120], 13);

            L.tileLayer('http://172.16.1.55:7780/Map/{z}/{xd}/{yd}/{x}_{y}_{z}.png', {//http://{s}.tile.osm.org/{z}/{x}/{y}.png  172.16.1.55:7780/Map/{z}/{xd}/{yd}/{x}_{y}_{z}.png
                attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            }).addTo(map);




        },

        printPoint : function(){

            var map = gis.map;
            for (var x = 120;x<121.005;) {
                x = x + 0.005
                for (var y = 30; y < 30.5;) {
                    y = y + 0.005
                    var circle = L.circle([y, x], {radius: 20}).addTo(map);
                    var obj = {'x': x, 'y': y}
                    gis.point_fire(circle, obj)
                }
            }
        },

        point_fire : function (circle,x) {
            circle.addEventListener('click',function () {
                gis.point_fire2(circle,x)
                console.log(x)
            })
        },

        point_fire2 : function (circle,x) {
            console.log(x)
        }
    }

    $.fn.Demo = function () {
        var gis = this;
        return new Demo(gis)
    }
})(jQuery)


转载自:https://blog.csdn.net/ShangQuan2012/article/details/72723248

You may also like...