2012-02-06 244 views
2

我想在OpenLayers中繪製國家位置,並且在更改默認圖標時遇到問題。在OpenLayers中更改默認標記圖標

我的基本代碼如下:

function addCountryMarker(ll, popupClass, popupContentHTML, closeBox, overflow) { 
     var feature = new OpenLayers.Feature(alumniCountries, ll); 
     feature.closeBox = closeBox; 
     feature.popupClass = popupClass; 
     feature.data.popupContentHTML = popupContentHTML; 
     feature.data.overflow = (overflow) ? "auto" : "hidden"; 

     var marker = feature.createMarker(); 

     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(); 
      } 
      currentPopup = this.popup; 
      OpenLayers.Event.stop(evt); 
     }; 
     marker.events.register("mousedown", feature, markerClick); 
     alumniCountries.addMarker(marker); 
    } 

而這一切工作正常,但顯示默認的OpenLayers圖標。下面的代碼行更改圖標,但它打破了彈出式的markerClick功能:

feature.icon = new OpenLayers.Icon("marker-blue.png"); 

使用marker.icon也同樣行更改圖標,但也打破彈出。非常感謝任何關於如何更改圖標而不打破彈出窗口的指針。

回答

5

過了一段時間,但它最終很簡單:

feature.data.icon = new OpenLayers.Icon("marker-blue.png"); 
0
function addMarker(ll, iconURL, popupClass, popupContentHTML, closeBox, overflow) { 
    try 
    { 
    var size = new OpenLayers.Size(32, 32); 
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h); 
    var newIcon = new OpenLayers.Icon(iconURL, size, offset); 
    var feature = new OpenLayers.Feature(markers, ll); 
    feature.closeBox = closeBox; 
    feature.popupClass = popupClass; 
    feature.data.popupContentHTML = popupContentHTML; 
    feature.data.overflow = (overflow) ? "auto" : "hidden"; 
    feature.data.icon = newIcon; 

    var marker = feature.createMarker(); 
    //marker.setUrl('http://maps.google.com/intl/en_us/mapfiles/ms/micons/orange-dot.png'); 

    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(); 
     } 
     currentPopup = this.popup; 
     OpenLayers.Event.stop(evt); 
    }; 
    marker.events.register("mousedown", feature, markerClick); 
    //marker.setUrl(''); 


    markers.addMarker(marker); 
} 
catch (err) { 
    txt = "There was an error in initOsmMap\n\n"; 
    txt += "Error description: " + err.message + "\n------------------------------------------------------------------------------------------------------------------------------------------------------------\n"; 
    console.log(txt); 
} 

}