0

私は次のようにデータを取得しています:

$.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から返された値の数と、サブレイヤーステートメントの正しい領域に入力された値に一致するように、これらの「サブレイヤー」の数を作成する必要があります。

4

2 に答える 2

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);
}
于 2015-03-27T00:46:30.580 に答える