var style = new OpenLayers.Style(
{
graphicWidth: 30,
graphicHeight: 30,
//graphicYOffset: -35,
label: "${name}",
fontSize: "9pt",
fontWeight: "bold",
labelYOffset: "-20"
},
{
rules: [
new OpenLayers.Rule({
elseFilter: true,
symbolizer: {
externalGraphic: "${img}"
}
})
]
}
);
var vector = new OpenLayers.Layer.Vector(vectorName, {
styleMap: new OpenLayers.StyleMap(style)
});
map.addLayer(vector);
//需要修改
var features = new Array(400);
var ic = "Images/Icon/car_yellow_0.png";
for (var i = 0; i < 400; i++) {
var sHtml = "<tr><td valign=\"top\">test</td><td valign=\"top\">" + i.toString() + "</td></tr>";
px = Math.random() * (122 - 121 + 0.001) + 121;
py = Math.random() * (30 - 29 + 0.001) + 29;
var name="test"+i.toString();
var ll = tranformTo900913(px, py);
features[i] = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(
lonlat.lon, lonlat.lat
), {
name: name,
img: ic,
html: sHtml
}
);
}
selectControl = new OpenLayers.Control.SelectFeature(vector); //添加点击标记事件
map.addControl(selectControl);
selectControl.activate();
vector.events.on({ //画popup窗
featureselected: function (e) {
feature = e.feature;
popup = new OpenLayers.Popup.FramedCloud("featurePopup",
feature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size(300, 300),
feature.attributes.html,
null, true, function (e) {
selectControl.unselect(this.feature);
});
feature.popup = popup;
popup.feature = feature;
map.addPopup(popup);
},
featureunselected: function (e) {
feature = e.feature;
if (feature.popup) {
popup.feature = null;
map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
}
}
});
vector.addFeatures(features);
}