我无法将从ajax调用获得的值赋给数组并传递该数组。
所以变量选项无效
我有这段代码
$.ajax({ url: url,
success: function(data){
for (var i = 0; i < data.length; i++) {
var object = data[i];
var array = JSON.parse(object.polygon);
var options = {
color: object.color,
fillColor: object.fill,
fillOpacity: object.opacity
};
var options_example = {
color: "#fff",
fillColor: "#000",
fillOpacity: "0.7"
};
console.log(options);// This Print correctly
console.log(options_example);// same as previous one
var land = L.polygon(array,options).addTo(map);//Not Work
var land = L.polygon(array,options_example).addTo(map);//Working
land.bindPopup("<b>"+object.number+"</b><br>"+object.area);
}
}});
响应:
[{"id":"2","number":"asd","area":"asd","polygon":"[[32.31948848454971,35.31074523925781],[32.33022285080643,35.32121658325195],[32.318908212283944,35.360355377197266],[32.29743552506946,35.30954360961914],[32.31948848454971,35.31074523925781]]","basin":"asd","color":""},{"id":"7","number":"adasd","area":"asd","polygon":"[[32.32129727814492,35.36873459815979],[32.321542074502524,35.36865413188934],[32.32107061426081,35.36771535873413],[32.32083488321952,35.368616580963135],[32.32129727814492,35.36893308162689],[32.32134714412331,35.36882042884826],[32.32129727814492,35.36873459815979]]","basin":"asdas","color":"#fff","fill":"#000","opacity":"0.7"}]
我不知道我在这里缺少什么
答案 0 :(得分:0)
在响应对象的数组的第一个元素中,字段fillColor
和fillOpacity
以及color
为空。
也许尝试一些默认的vaules:
var options = {
color: object.color || "#fff",
fillColor: object.fill || "#000",
fillOpacity: object.opacity || "0.7"
};