2013-10-22 103 views
1

我正在嘗試在鼠標懸停上更改地標圖像。下面是鼠標懸停事件處理程序:Google Earth API在鼠標懸停上更改地標圖像

function changePlacemark(e){ 
    e.preventDefault(); 
    this.getStyleSelector().getIconStyle().getIcon().setHref('myImageURL'); 
} 

的問題是,該功能運行時,它確實改變形象,但它也完全復位標使其一遍,就好像是做縮放動畫一個全新的地標被添加到地圖中。

有沒有辦法來防止這種情況發生?我正在尋找只更改圖像,而不是重置地標。那種破壞體驗。

回答

2

當然,您可以使用樣式在KML或通過api創建滾動圖標。您只需設置功能的樣式,而不是更新圖標的href屬性。

在API:

var style = ge.createStyle(""); 
    style.getIconStyle().getIcon().setHref('myImageURL'); 
    feature.setStyleSelector(style); 

在KML:

<Document> 
    <Style id="highlightPlacemark"> 
     <IconStyle> 
     <Icon> 
      <href>http://maps.google.com/mapfiles/kml/paddle/red-stars.png</href> 
     </Icon> 
     </IconStyle> 
    </Style> 
    <Style id="normalPlacemark"> 
     <IconStyle> 
     <Icon> 
      <href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href> 
     </Icon> 
     </IconStyle> 
    </Style> 
    <StyleMap id="exampleStyleMap"> 
     <Pair> 
     <key>normal</key> 
     <styleUrl>#normalPlacemark</styleUrl> 
     </Pair> 
     <Pair> 
     <key>highlight</key> 
     <styleUrl>#highlightPlacemark</styleUrl> 
     </Pair> 
    </StyleMap> 
    <Placemark> 
     <name>Roll over this icon</name> 
     <styleUrl>#exampleStyleMap</styleUrl> 
     <Point> 
     <coordinates>-122.0856545755255,37.42243077405461,0</coordinates> 
     </Point> 
    </Placemark> 
    </Document>