OpenLayers 标记图标问题

发布于 2024-09-24 14:43:25 字数 4227 浏览 5 评论 0原文

我正在我的网站中创建 OSM 地图并使用 openlayer 添加一些标记。我的问题是我总是得到相同的标记图标。

 <script type="text/javascript">

 var map = new OpenLayers.Map("heatMap");
    var mapnik = new OpenLayers.Layer.OSM();
    map.addLayer(mapnik);
    map.setCenter(new OpenLayers.LonLat(-4.0575942987957,54.390175926091) // Center of the map
      .transform(
        new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
        new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
      ), 6 // Zoom level
    );

    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);

    var icon1 = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png', size, offset);
    var icon2 = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker-gold.png', size, offset);
    var icon3 = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker-green.png', size, offset);

    var lonLat1 = new OpenLayers.LonLat(-4.0575942987957,54.390175926091).transform(
                            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
                            map.getProjectionObject() // to Spherical Mercator Projection
                            );

    var lonLat2 = new OpenLayers.LonLat(-3.0575942987957,54.390175926091).transform(
                            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
                            map.getProjectionObject() // to Spherical Mercator Projection
                            );

    var lonLat3 = new OpenLayers.LonLat(-5.0575942987957,54.390175926091).transform(
                            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
                            map.getProjectionObject() // to Spherical Mercator Projection
                            );

    var marker1 = new OpenLayers.Marker(lonLat1);
    var feature = new OpenLayers.Feature(markers, lonLat);
    feature.closeBox = true;
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, { autoSize: true });
    feature.data.popupContentHTML = '<p>Marker1<p>';
    feature.data.overflow = "hidden";

    marker1.feature = feature;

    var markerClick = function (evt) {
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
        }
        OpenLayers.Event.stop(evt);
    };
    marker1.events.register("mousedown", feature, markerClick);

    var marker2 = new OpenLayers.Marker(lonLat2);
    var feature = new OpenLayers.Feature(markers, lonLat);
    feature.closeBox = true;
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, { autoSize: true });
    feature.data.popupContentHTML = '<p>Marker2<p>';
    feature.data.overflow = "hidden";

    marker2.feature = feature;

    var markerClick = function (evt) {
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
        }
        OpenLayers.Event.stop(evt);
    };
    marker2.events.register("mousedown", feature, markerClick);

    var marker3 = new OpenLayers.Marker(lonLat3);
    var feature = new OpenLayers.Feature(markers, lonLat);
    feature.closeBox = true;
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, { autoSize: true });
    feature.data.popupContentHTML = '<p>Marker 3<p>';
    feature.data.overflow = "hidden";

    marker3.feature = feature;

    var markerClick = function (evt) {
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
        }
        OpenLayers.Event.stop(evt);
    };
    marker3.events.register("mousedown", feature, markerClick);

    markers.addMarker(marker1, icon1);

    markers.addMarker(marker2, icon2);

    markers.addMarker(marker3, icon3);

现在的问题是所有三个标记都有相同的图标...有人可以帮忙吗我..

I'm creating OSM map in my web site and adding some markers with openlayer.. My problem is that I allways get the same icon for marker..

 <script type="text/javascript">

 var map = new OpenLayers.Map("heatMap");
    var mapnik = new OpenLayers.Layer.OSM();
    map.addLayer(mapnik);
    map.setCenter(new OpenLayers.LonLat(-4.0575942987957,54.390175926091) // Center of the map
      .transform(
        new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
        new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
      ), 6 // Zoom level
    );

    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);

    var icon1 = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png', size, offset);
    var icon2 = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker-gold.png', size, offset);
    var icon3 = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker-green.png', size, offset);

    var lonLat1 = new OpenLayers.LonLat(-4.0575942987957,54.390175926091).transform(
                            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
                            map.getProjectionObject() // to Spherical Mercator Projection
                            );

    var lonLat2 = new OpenLayers.LonLat(-3.0575942987957,54.390175926091).transform(
                            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
                            map.getProjectionObject() // to Spherical Mercator Projection
                            );

    var lonLat3 = new OpenLayers.LonLat(-5.0575942987957,54.390175926091).transform(
                            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
                            map.getProjectionObject() // to Spherical Mercator Projection
                            );

    var marker1 = new OpenLayers.Marker(lonLat1);
    var feature = new OpenLayers.Feature(markers, lonLat);
    feature.closeBox = true;
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, { autoSize: true });
    feature.data.popupContentHTML = '<p>Marker1<p>';
    feature.data.overflow = "hidden";

    marker1.feature = feature;

    var markerClick = function (evt) {
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
        }
        OpenLayers.Event.stop(evt);
    };
    marker1.events.register("mousedown", feature, markerClick);

    var marker2 = new OpenLayers.Marker(lonLat2);
    var feature = new OpenLayers.Feature(markers, lonLat);
    feature.closeBox = true;
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, { autoSize: true });
    feature.data.popupContentHTML = '<p>Marker2<p>';
    feature.data.overflow = "hidden";

    marker2.feature = feature;

    var markerClick = function (evt) {
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
        }
        OpenLayers.Event.stop(evt);
    };
    marker2.events.register("mousedown", feature, markerClick);

    var marker3 = new OpenLayers.Marker(lonLat3);
    var feature = new OpenLayers.Feature(markers, lonLat);
    feature.closeBox = true;
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, { autoSize: true });
    feature.data.popupContentHTML = '<p>Marker 3<p>';
    feature.data.overflow = "hidden";

    marker3.feature = feature;

    var markerClick = function (evt) {
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
        }
        OpenLayers.Event.stop(evt);
    };
    marker3.events.register("mousedown", feature, markerClick);

    markers.addMarker(marker1, icon1);

    markers.addMarker(marker2, icon2);

    markers.addMarker(marker3, icon3);

Now the problem is that all three markers have the same icon... Can somebody help me..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-10-01 14:43:25

创建标记实例后更改图标。

marker = new ....
var size = new OpenLayers.Size(25, 12);
var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
marker.icon = new OpenLayers.Icon("marker.png", size, offset);
//UPDATE AGAIN
marker.icon.size = size;
marker.icon.offset = offset;
marker.draw();

Change icon after creating marker instance.

marker = new ....
var size = new OpenLayers.Size(25, 12);
var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
marker.icon = new OpenLayers.Icon("marker.png", size, offset);
//UPDATE AGAIN
marker.icon.size = size;
marker.icon.offset = offset;
marker.draw();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文