简体   繁体   English

如何改变openlayers marker icon?

[英]how to change openlayers marker icon?

I am using Openlayers to my application. 我在我的应用程序中使用Openlayers。

i have succeeded in showing marker when i click on the map, but now i want to change the marker icon when i click on that, how to do this. 当我点击地图时,我已成功显示标记,但现在我想在点击它时更改标记图标,如何执行此操作。 I didn't find any method like marker.setIcon(); 我没有找到像marker.setIcon();

i am really confusing here. 我真的很困惑。

map.events.register("click", map , function(e){
   var opx = map.getLonLatFromPixel(e.xy) ;
   var marker = new OpenLayers.Marker(opx);
   markers.addMarker(marker);
   marker.events.register("click", marker, function(e){
    //here i want to change the icon to some other 
    popup = new OpenLayers.Popup.FramedCloud("chicken",
                         marker.lonlat,
                         new OpenLayers.Size(200, 200),
                         "example popup",
                         null, true);

     map.addPopup(popup);
       }); 
    // here i want to change to default.
  });

The documentation at OpenLayers 2 : OpenLayers 2的文档:

http://dev.openlayers.org/docs/files/OpenLayers/Marker-js.html http://dev.openlayers.org/docs/files/OpenLayers/Marker-js.html

mentions the method setUrl to set the icon image. 提到方法setUrl来设置图标图像。 This should work: 这应该工作:

marker.setUrl('marker.png');

I got answer from here , here is the code 我从这里得到答案,这是代码

marker.events.register('click', marker, function() {
  // Reset all markers.
  for (var i=0; i<markers.length; i++) {
    markers[i].icon.imageDiv.firstChild.setAttribute(
      'src', 'images/red.png');
  }
  this.icon.imageDiv.firstChild.setAttribute('src', 'images/blue.png');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM