2016-07-27 89 views
0

我想設置標籤的顏色,但我不認爲setLabelColor()是一個內置功能:谷歌地圖API setLabelColor

google.maps.event.addListener(marker, 'click', (function(marker, i) { 
    return function() { 
     infowindow.setContent(amenities[i][0]); 
     infowindow.open(map, marker); 
     for (j = 0; j < gmarkers.length; j++) { 
      gmarkers[j].setIcon(blackCircle); //resets default icon when another is clicked 
      gmarkers[j].setLabelColor('white'); 
     } 
     marker.setIcon(whiteCircle); //Sets clicked icon 
     marker.setLabelColor('black'); 
    } 
})(marker, i)); 

我得到一個錯誤:

Uncaught TypeError: gmarkers[j].setLabelColor is not a function 

我知道setLabel()是一個函數,但我只想改變顏色,標籤本身是好的,因爲它是?!

+0

確定'gmarkers'有標記?我認爲問題不在於你的代碼的這一部分。 –

+0

是的,我確定。如果我刪除'gmarkers [j] .setLabelColor('white');'和'marker.setLabelColor('black')''行,則代碼正常工作。 – hrtestrt

+0

你能創建一個包括完整代碼的小提琴嗎? –

回答

0

使用setLabel的標籤設置爲MarkerLabel對象

要跟蹤標籤,所以你可以把它一樣,一個選擇是將其保存在標記的自定義屬性(下面的例子使用._labelText) 。

marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(amenities[i][1], amenities[i][2]), 
    map: map, 
    icon: blackCircle, //Sets default icon 
    zIndex: -100, 
    label: { 
    text: "" + amenities[i][3], 
    color: '#EDEBDD' 
    }, 
    _labelText: "" + amenities[i][3] 
}); 
gmarkers.push(marker); 
google.maps.event.addListener(marker, 'click', (function(marker, i) { 
    return function() { 
    infowindow.setContent(amenities[i][0]); 
    infowindow.open(map, marker); 
    for (j = 0; j < gmarkers.length; j++) { 
     gmarkers[j].setIcon(blackCircle); //resets default icon when another is clicked 
     gmarkers[j].setLabel({ 
     color: 'white', 
     text: gmarkers[j]._labelText 
     }); 
     gmarkers[j].setZIndex(-100); 
    } 
    this.setIcon(whiteCircle); //Sets clicked icon 
    this.setLabel({ 
     color: 'black', 
     text: this._labelText 
    }); 
    this.setZIndex(100); 
    } 
})(marker, i)); 

updated fiddle

+0

感謝這個,我實現了這裏: https://jsfiddle.net/5aa9jtax/3/ 但它弄亂被點擊一個編號系統? – hrtestrt

+0

在您的問題得到解答之前,發佈[最小,完整,測試和可讀示例](http://stackoverflow.com/help/mcve)**會對您更有用。要解決此問題,您需要保留有關標記標籤的信息。 – geocodezip