Javascript 如何为每个功能添加属性
我的项目是地理地图应用。 这是我的协议:Javascript 如何为每个功能添加属性,javascript,maps,openlayers,Javascript,Maps,Openlayers,我的项目是地理地图应用。 这是我的协议: var myprotocol = new OpenLayers.Protocol.HTTP({ url: url, format: new OpenLayers.Format.GeoJSON({ extractStyles: true, extractAttributes: true }) }); 这是我的图层功能: var layer = new OpenLayers.Layer.Ve
var myprotocol = new OpenLayers.Protocol.HTTP({
url: url,
format: new OpenLayers.Format.GeoJSON({
extractStyles: true,
extractAttributes: true
})
});
这是我的图层功能:
var layer = new OpenLayers.Layer.Vector("layerName", {
projection: epsg4326,
strategies: strategies,
protocol: myprotocol,
displayInLayerSwitcher: true
});
layer.events.register('loadend', layer, function (evt) {
console.log("onloadend was called");
});
layer.events.register('loadstart', layer, function (evt) {
console.log("onloadstart was called");
}
myprotocol.read({
// maxFeatures: 100,
callback: function (resp) {
console.log("read _CallBack was called");
console.log(resp);
}
});
myprotocol.handleResponse = function (resp, opt) {
console.log("handleResponse was called");
};
这是我的另一个功能:
var layer = new OpenLayers.Layer.Vector("layerName", {
projection: epsg4326,
strategies: strategies,
protocol: myprotocol,
displayInLayerSwitcher: true
});
layer.events.register('loadend', layer, function (evt) {
console.log("onloadend was called");
});
layer.events.register('loadstart', layer, function (evt) {
console.log("onloadstart was called");
}
myprotocol.read({
// maxFeatures: 100,
callback: function (resp) {
console.log("read _CallBack was called");
console.log(resp);
}
});
myprotocol.handleResponse = function (resp, opt) {
console.log("handleResponse was called");
};
以下是我的特点:
var data = new Object();
data.iconUrl = katman.iconUrl;
data.fillColor = katman.fillColor;
data.fillOpacity = katman.fillOpacity;
data.graphicHeight = katman.graphicHeight;
data.graphicOpacity = katman.graphicOpacity;
data.graphicWidth = katman.graphicWidth;
data.graphicXOffset = katman.graphicXOffset;
data.graphicYOffset = katman.graphicYOffset;
data.pointRadius = katman.pointRadius;
data.pointerEvents = katman.pointerEvents;
data.rotation = katman.rotation;
data.strokeColor = katman.strokeColor;
data.strokeDashstyle = katman.strokeDashstyle;
data.strokeLinecap = katman.strokeLinecap;
data.strokeOpacity = katman.strokeOpacity;
data.strokeWidth = katman.strokeWidth;
data.layerId = feature.data.layerId;
data.shapeId = feature.data.shapeId;
我想为从web服务收到的featurecollection对象的每个功能添加此属性。我已使用以下代码解决了此问题。谢谢您的帮助
myprotocol.handleResponse = function (resp, opt) {
console.log("handleResponse was called");
var features = geojson.read(resp.priv.responseText);
for (var i in features) {
var feature = features[i];
feature.attributes.iconUrl = katman.iconUrl;
feature.attributes.fillColor = katman.fillColor;
feature.attributes.fillOpacity = katman.fillOpacity;
feature.attributes.graphicHeight = katman.graphicHeight;
feature.attributes.graphicOpacity = katman.graphicOpacity;
feature.attributes.graphicWidth = katman.graphicWidth;
feature.attributes.graphicXOffset = katman.graphicXOffset;
feature.attributes.graphicYOffset = katman.graphicYOffset;
feature.attributes.pointRadius = katman.pointRadius;
feature.attributes.pointerEvents = katman.pointerEvents;
feature.attributes.rotation = katman.rotation;
feature.attributes.strokeColor = katman.strokeColor;
feature.attributes.strokeDashstyle = katman.strokeDashstyle;
feature.attributes.strokeLinecap = katman.strokeLinecap;
feature.attributes.strokeOpacity = katman.strokeOpacity;
feature.attributes.strokeWidth = katman.strokeWidth;
}
layer.addFeatures(features);
};
为什么要给它加上标签呢?我用c来表示web服务codeRamazan,你也用键盘。为什么不使用tag:):)但我认为这没关系。我正在使用c#作为web服务。也许其他人想知道这个细节:)