G'day!
I'd like to add a unique (css-)class to every single marker on my OpenLayers map, but i don't know how! I tried pretty much everything and also posted this question at the forums of OpenStreetMap (as the guys over there are pretty familiar with OpenLayers).
This is the function i wrote to add markers:
function ownMarker(lon,lat,icon,markerid) { var size = new OpenLayers.Size(38,58); var offset = new OpenLayers.Pixel(-(size.w/2), -size.h); var element = new OpenLayers.Element.addClass('div', 'test'); var icon = new OpenLayers.Icon('images/marker/'+ icon +'.png', size, offset); var marker = new OpenLayers.Marker((new OpenLayers.LonLat(lon, lat).transform(proj4326, projmerc)),icon) marker.id = markerid; marker.events.register("mousedown", marker, function() { if(this.id != 'home') { currentID = this.id; $('.activeResult').removeClass('activeResult'); $('#results a[href$=' + this.id + ']').addClass("activeResult"); showContent($(".nav a:first-child").attr("href")); $(".nav a:first-child").addClass("activeContent") if(contentOpen == false){ $("#container").show(); $(".frame").animate({left: 0}); contentOpen = true; } tempLatitude = $('#results a[href$=' + this.id + ']').attr("lat"); tempLongitude = $('#results a[href$=' + this.id + ']').attr("lon"); targetLatitude = $('#results a[href$=' + this.id + ']').attr("lat"); targetLongitude = $('#results a[href$=' + this.id + ']').attr("lon"); ownPanTo(tempLongitude, tempLatitude); console.log(this.id); } }); markers.addMarker(marker); }
As you can see in line 5, i already tried to add a class to the marker - without success!
Please help me! :)
Cheers!
Maybe the answer is just too obvious: An OpenLayers Icon is no HTML element in the first place but you can get access to an HTML element that will be part of the Icon when drawn on the map: http://dev.openlayers.org/docs/files/OpenLayers/Icon-js.html#OpenLayers.Icon.imageDiv (that is not what comes up when I used the very first link to api docs on the www.openlayers.org, strange) So one might solve this problem like this:
icon.imageDiv.className += " my_icon_class";
来源:https://stackoverflow.com/questions/15561988/openlayers-adding-a-css-class-to-a-marker