我正在尝试循环返回的JSON对象并根据x和y坐标绘制热图。以下是我设置热图的方法:
function getHeatMap() {
heatLayer = new HeatmapLayer({
config: {
"useLocalMaximum": true,
"radius": 40,
"gradient": {
0.45: "rgb(000,000,255)",
0.55: "rgb(000,255,255)",
0.65: "rgb(000,255,000)",
0.95: "rgb(255,255,000)",
1.00: "rgb(255,000,000)"
}
},
"map": map,
"domNodeId": "heatLayer",
"opacity": 0.85
});
$.ajax({
url: "index.aspx/getBusCommuter",
type: "POST",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var parsed = JSON.parse(data.d);
var data = [];
$.each(parsed, function (i, jsondata) {
var coordXicon = jsondata.BusStopX;
var coordYicon = jsondata.BusStopY;
data = [
{
attributes: {},
geometry: {
spatialReference: { wkid: 102100 },
type: "point",
x: coordXicon,
y: coordYicon
}
}
];
});
heatLayer.setData(data);
map.addLayer(heatLayer);
},
error: function (request, state, errors) {
}
});
}
基本上,JSON对象将循环到最后一组JSON对象,并仅绘制最后一组而不是全部。我想知道我逻辑的哪一部分是错的。
提前致谢。
答案 0 :(得分:1)
问题在于:
data = [
{
attributes: {},
geometry: {
spatialReference: {
wkid: 102100
},
type: "point",
x: coordXicon,
y: coordYicon
}
}
];
当您可能只想将新元素推送到最后时,您正在为每个项目设置整个数据数组(覆盖所有旧数据)。试试这个:
data.push({ ... });