OpenLayers是否包含一个函数来根据它的ID返回标记引用?这是我想要工作的代码片段(我用注释标记为“HERE!”):我试图使用命令“getMarkerById(this.id)”检索标记,并且firebug通知我返回价值未定义。然后我尝试使用其中一个标记(“41014”)的静态ID号,但也没有用。任何帮助将不胜感激!
$(function()
{
var map, layer;
//alert("RAN!!");
map = new OpenLayers.Map("map");
var wms = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0",
{'layers':'basic'} );
map.addLayer(wms);
map.setCenter(new OpenLayers.LonLat(-40, 30), 2.5);
var markers = new OpenLayers.Layer.Markers("Markers");
map.addLayer(markers);
//load and retrieve values from local XML file here
var xmlDoc=loadXMLDoc("http://whxlab3.dart.ns.ec.gc.ca/~brinka/EMETsiteV1/buoys.xml");
var z=xmlDoc.getElementsByTagName("id");
var y=xmlDoc.getElementsByTagName("lat");
var x=xmlDoc.getElementsByTagName("lng");
for(i=0; i<x.length; i++)
{
//use loaded XML file to retrieve values for marker positions
var px = x[i].childNodes[0].nodeValue;
var py = y[i].childNodes[0].nodeValue;
var pz = z[i].childNodes[0].nodeValue;
var size = new OpenLayers.Size(32, 37);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('stock_images/buoy.png', size, offset);
icon.setOpacity(0.85);
var lonlat = new OpenLayers.LonLat(px, py);
var marker = new OpenLayers.Marker(lonlat, icon);
marker.id=(pz);
marker.events.register("mouseover", marker, function(){
console.log("Over the marker "+this.id+" at place "+this.lonlat);
this.inflate(1.2);
this.setOpacity(1);
});
marker.events.register("mouseout", marker, function(){
console.log("Out the marker "+this.id+" at place "+this.lonlat);
this.inflate(1/1.2);
this.setOpacity(0.85);
});
marker.events.register("click", marker, function(){
console.log("Clicked "+this.id+" at place "+this.lonlat);
console.log("Z value here: ", this.id);
popup = new OpenLayers.Popup.FramedCloud("chicken", this.lonlat, new OpenLayers.Size(200, 200), ("Buoy Number: "+this.id), null, true);
map.addPopup(popup);
markers.removeMarker(getMarkerById(this.id)); //HERE!
});
markers.addMarker(marker);
}
});
答案 0 :(得分:0)
没有, 但您可以在(全局)对象中定义标记,如:
obj.marker = new OpenLayers.Marker(lonlat, icon);
而不是添加到标记图层(例如“标记”):
markers.addMarker(obj.marker);
如果要删除它,请执行以下操作:
markers.removeMarker(obj.marker);
答案 1 :(得分:0)
map = new OpenLayers.Map("map");
var wms = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0",
{'layers':'basic'} );
map.addLayer(wms);
map.setCenter(new OpenLayers.LonLat(-40, 30), 2.5);
标记图层添加(矢量)
var markerlayer = new OpenLayers.Layer.Vector("Marker Layers", {
styleMap: new OpenLayers.StyleMap({
'default': {
externalGraphic: "stock_images/buoy.png",
graphicWidth:32,//pixel
graphicHeight:37,//pixel
graphicOpacity:"${opacity}"
}
})
});
map.addLayer(markerlayer);
var xmlDoc=loadXMLDoc("http://whxlab3.dart.ns.ec.gc.ca/~brinka/EMETsiteV1/buoys.xml");
var z=xmlDoc.getElementsByTagName("id");
var y=xmlDoc.getElementsByTagName("lat");
var x=xmlDoc.getElementsByTagName("lng");
有:
for(i=0; i<x.length; i++)
{
var px = x[i].childNodes[0].nodeValue;
var py = y[i].childNodes[0].nodeValue;
var pz = z[i].childNodes[0].nodeValue;
var lonlat = new OpenLayers.LonLat(px, py);
var marker= new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat));
// var marker= new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(px, py));
marker.attributes = {
opacity: "1"
};
markerlayer.addFeatures([marker]);
};
点击功能
markercontrol = new OpenLayers.Control.SelectFeature(markerlayer, { eventListeners: { featurehighlighted: controlfunction } })
map.addControl(markercontrol);
function controlfunction(e) {
var featureID = e.feature.id;
var marker = markerlayer.getFeatureById(featureID)
marker.attributes.opacity = '0.5';//opacity
markerlayer.removeFeatures(marker);//remove
console.log(featureID);
console.log(e);
}