添加多个语句以实现功能

时间:2015-03-27 00:39:25

标签: javascript jquery ajax cartodb

我的数据是这样的:

$.ajax({
            url: 'php/get_all_attri.php',
            type: 'GET',
            dataType: 'JSON',
            data: {
                col_name: firstSel
            },
            success: function(data) {
                var total_statement = {};
               $.each(data, function(i, data) {
                console.log(data.attri_1);
            });

                }
            });

在控制台中,这将返回查询的可能值,例如18 19 20 21等。

它们也可以是TRUE FALSE字符串。

我需要的是为这些值创建用于使用cartoDB / leaflet构建地图的语句。这是功能:

function goMap3() {

        var layerSource = {
            user_name: 'me',
            type: 'cartodb',
            sublayers: [{
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='" + attr_value + "'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            }, ]
        };
        cartodb.createLayer(map_object, layerSource, options)
            .addTo(map_object);
    }

我需要发生的是整个部分被写入多次,因为我的AJAX返回了不同的值。

          {
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='" + attr_value + "'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            },

在这种情况下,attr_value每次都需要用AJAX中的值替换。这将允许我的整个函数看起来像这样:

function goMap3() {

        var layerSource = {
            user_name: 'me',
            type: 'cartodb',
            sublayers: [{
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='17'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            }, 
  {
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='18'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            },
  {
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='19'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            },]
        };
        cartodb.createLayer(map_object, layerSource, options)
            .addTo(map_object);
    }

总结:我试图弄清楚如何最终得到我在问题中的最后一个功能。我需要制作一些"子层"匹配从我的AJAX返回的值的数量和填充在子层语句中正确区域的值。

2 个答案:

答案 0 :(得分:1)

您可能希望为数组中的每个条目循环。只需将数据发送到此函数,它将创建对象

   function goMap3(data) {

    var layerSource = {
        user_name: 'me',
        type: 'cartodb',
        sublayers: []
    };

    //Adds a new sublayer to the layerSource object every iteration. 
    $.each(data, function (i, attr_value) {
        layerSource.sublayers.push({
            sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='" + attr_value + "'",
            cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker- line-color:#fff;marker-allow-overlap: true;"
        });
    });

    cartodb.createLayer(map_object, layerSource, options)
        .addTo(map_object);
}

答案 1 :(得分:1)

这些不是多个语句,它们只是数组的元素。您只需在$.each()循环中添加到数组中:

function goMap3(data) {

    var sublayers = [];
    $.each(data, function(i, attr_value) {
        sublayers.push({
            sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='" + attr_value + "'",
            cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"
        });
    });

    var layerSource = {
        user_name: 'me',
        type: 'cartodb',
        sublayers: sublayers
    };
    cartodb.createLayer(map_object, layerSource, options)
        .addTo(map_object);
}

然后你可以从AJAX成功函数中调用geoMap3(data)