2015-06-14 188 views
0

谷歌地圖更改圖標我需要創建一個更改標誌(我的地圖有3個標誌)時,在外部按鈕當我點擊一個按鈕

用戶的點擊例如,地圖是這個,當你打開一個交互式地圖現場:

enter image description here

當你點擊按鈕1,在這張照片中

enter image description here

僅更改圖標像

你能幫助我的JavaScript代碼? :) 這是我的HTML代碼

<div id="mapMeteo"></div> 
<br> 
<button id="btn1">Bottone1</button> <button id="btn2">Bottone2</button> <button id="btn3">Bottone3</button> 

這是谷歌的JavaScript代碼:

<script> 
    function initialize() { 
     //Marker personalizzato 
     var imageMM = 'images/markerMM.png'; 
      imageMMM = 'images/markerMMM.png'; 
      imageMC = 'images/markerMC.png'; 

     //mappa Meteo 
     var mapMeteo = document.getElementById('mapMeteo'); 
     var Latlng = new google.maps.LatLng(42.9254851, 13.8632125); 
     var mapMOptions = { 
      center: Latlng, 
      zoom: 12, 
      mapTypeControl: false, 
      draggable: false, 
      scaleControl: false, 
      scrollwheel: false, 
      navigationControl: false, 
      streetViewControl: false, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     var mapM = new google.maps.Map(mapMeteo, mapMOptions); 

     //Marker 1 
     var marker1 = new google.maps.Marker({ 
      position: new google.maps.LatLng(42.9547985, 13.958793), 
      map: mapM, 
      icon: imageMMM 
     }); 

     var marker2 = new google.maps.Marker({ 
      position: new google.maps.LatLng(42.9222113, 13.9199294), 
      map: mapM, 
      icon: imageMM 
     }); 

     var marker3 = new google.maps.Marker({ 
      position: new google.maps.LatLng(42.8832739, 13.9438162), 
      map: mapM, 
      icon: imageMC 
     }); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

,這是我的jQuery代碼,但不更改圖標

$("#btn1").click(function(){ google.maps.event.addListener(marker1, 'click', function() { marker1.setIcon(imageMM); infowindow.open(mapM); }); });

回答

1

您可以使用dom偵聽器和Marker類的setIcon方法。

https://developers.google.com/maps/documentation/javascript/reference#methods_48

例如:

var btn1 = document.getElementById('btn1'); 

google.maps.event.addDomListener(btn1, 'click', function() { 

    marker1.setIcon(imageMC); 
}); 

JSFiddle demo

+0

不起作用此代碼:(不改變圖標... –

+0

它工作正常,參見[這裏](HTTP ://jsfiddle.net/upsidown/mxz0tqn1/) – MrUpsidown

+0

我看到了,但我有5個標記,並且此代碼不能用於多個標記 –