我从数据库获取到一个json数组,想要把里面点显示在图层上。数组获取没问题,alert过 geolongitude和 geolatitude也都存在
lbpoint定义过了是“保护站点”,就是显示不了,显示了一个坐标不知道哪来的点
$.ajax({
type : "GET",//请求方式
async : false,
url : "../services/"+options.datatype+"/"+options.method+"=" + options.lb,
data : "json",//数据类型text xml json script jsonp
success : function(msg) {//返回的参数就是 action里面所有的有get和set方法的参数
$(msg).each(function (i, val) {
val.img=options.img;
});
that.loaddata2dpoint(msg);
//that.loaddata3d(msg);
}
});
MapBztc.prototype.loaddata2dpoint = function (result) {
var that = this;
that.id=pointlb;
var customStyleFunction = function (feature, resolution) {
// 根据空气级别,设定不同颜色
var img="";
var mc="";
if(feature.data) {
img='images/menu/u1271small.png';/*+feature.data.img*/
//alert(img)
}else{
img='images/menu/u1271small.png';
}
if(feature.data) {
mc=feature.data.descript;
}else{
mc="";
}
//
//console.info(feature.data);
var fontSize = '14';
var styleimg = new ol.style.Style({
fill: new ol.style.Fill({ //矢量图层填充颜色,以及透明度
color: 'rgba(231, 216, 0, 0.5)'
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),image: new ol.style.Icon({
src: img
}),
});
var styletext = new ol.style.Style({
text: new ol.style.Text({
textAlign: "center",
textBaseline: "middle",
font: '14px Normal Arial',
text: mc, //that.mapoptions.key + feature.get('features')[0].data[that.mapoptions.value] + that.mapoptions.unit,//,// //feature.data.station,
fill: new ol.style.Fill({
color: "#00E400",////
}),
stroke: new ol.style.Stroke({
color: '#000000',
width: 1
}),
offsetX: 0,
offsetY: 20,
rotation: 0
})
});
style = [styleimg, styletext];
// console.info(style);
return style;
};
//console.info(result);
$(result).each(function (i, val) {
//判断类型,开始添加元素
var geolatitude= val.latitude;
var geolongitude= val.longitude;
var geom;
//alert("1")
geom = new ol.geom.Point([parseFloat(geolatitude),parseFloat(geolongitude)]);
feature = new ol.Feature(geom);
feature.data = val;
feature.layerid = that.id;
// feature.setStyle(iconStyle);
that.ol2dlist[that.id] = feature;
});
// Source and vector layer
//console.info(that.ol2dlist);
var vectorSource = new ol.source.Vector({
features: _.values(that.ol2dlist)
});
var tmpLayer = that.ol2d.getLayerById(that.id);
if (tmpLayer == null) {
alert("1")
tmpLayer = new ol.layer.Vector({
id: that.id,
opacity: 0.95,
maxzoom: 1224,
minzoom: 0.0001,
source: vectorSource,
style: customStyleFunction
});
console.info(that.ol2dlist)
console.info(tmpLayer)
that.ol2d.olmap.addLayer(tmpLayer);
} else {
alert("2")
tmpLayer.setSource(null);
tmpLayer.setStyle(customStyleFunction);
tmpLayer.setSource(vectorSource);
}
}