OpenLayers:向标记添加(css-)类吗? [英] OpenLayers: Adding a (css-)class to a marker?

查看:272
本文介绍了OpenLayers:向标记添加(css-)类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G'day!
我想为OpenLayers地图上的每个标记添加一个唯一的(css-)类,但是我不知道如何!我几乎尝试了所有事情,并将这个问题发布在 OpenStreetMap论坛 (因为那里的家伙对OpenLayers相当熟悉).
这是我编写的用于添加标记的功能:

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);
    }

正如您在第5行中看到的那样,我已经尝试将类添加到标记中-没有成功!

As you can see in line 5, i already tried to add a class to the marker - without success!

请帮助我! :)

干杯!

推荐答案

也许答案太明显了:OpenLayers Icon首先不是HTML元素,但是您可以访问将成为其中一部分的HTML元素在地图上绘制时的图标: http://dev.openlayers.org/docs/files/OpenLayers/Icon-js.html#OpenLayers.Icon.imageDiv (当我使用www.openlayers上的api文档的第一个链接时就不会出现这种情况.org(很奇怪),因此可以这样解决这个问题:

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";

这篇关于OpenLayers:向标记添加(css-)类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆