将CartoDb图层添加到Leaflet图层控件

时间:2013-06-27 13:25:29

标签: leaflet cartodb

我正试图在Leaflet地图上切换CartoDb图层的显示。我已经能够使用以下代码加载图层:

       var layerUrl = 'http://ronh-aagis.cartodb.com/api/v1/viz/rotaryclubs_geo2/viz.json';

       var clubPts = cartodb.createLayer(map, layerUrl, {
           // The ST_AsGeoJSON(ST_Simplify(the_geom,.01)) as geometry will store a simplified GeoJSON representation of each polygon as an attribute we can pick up on hover

           query: 'select  *, ST_AsGeoJSON(the_geom) as geometry from {{table_name}}',

           interactivity: 'cartodb_id, geometry'
       })

                          .on('done', function(layer) {
                              map.addLayer(layer);

                          layer.on('featureOver', function(e, pos, latlng, data) {
                       $('.leaflet-container').css('cursor','pointer');

                       if (data.cartodb_id != point.cartodb_id) {
                           drawHoverPoint(data);
                       }
                       cartodb.log.log(pos, data);
                   });

                   layer.on('featureOut', function(e, pos, latlng, data) {
                       $('.leaflet-container').css('cursor','default')
                       removePoint();
                   });

                   layer.on('error', function(err) {
                       cartodb.log.log('error: ' + err);
                   });

               }).on('error', function() {
                   cartodb.log.log("some error occurred");
               });

然而,当我尝试将此图层添加到图层控件时:

       var clubs = new L.LayerGroup();
       clubs.addLayer(clubPts);

我得到一个“未捕获的TypeError:对象#没有方法'onAdd'”错误。

有什么想法?谢谢!

1 个答案:

答案 0 :(得分:2)

降低复杂性并快速加速的好方法是使用已经构建的Leaflet插件,如Vector Layers,它已经内置了CartoDB支持。看看这里的演示。 http://jasonsanford.github.io/leaflet-vector-layers/demos/cartodb/

相关问题