我得到的数据如下:
$.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等。
它们也可以是字符串,如真假。
我需要的是这些值创建语句,这些语句用于使用cartoDB/传单构建映射。以下是功能:
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;"}
},在这种情况下,每次都需要用AJAX的值替换attr_value。这将使我的整个功能看起来如下所示:
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返回的值的数量,以及在子层语句中填入正确区域的值。
发布于 2015-03-27 00:49:01
这些不是多个语句,它们只是数组的元素。您可以简单地添加到$.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)。
发布于 2015-03-27 00:46:30
您可能希望为array.Just中的每个条目循环,将数据发送到此函数,它将创建该对象。
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);
}https://stackoverflow.com/questions/29291394
复制相似问题