2012-03-16 67 views
1

我對編碼非常陌生,實際上剛剛開始學習,所以我可以製作我目前正在處理的地圖。我一直在努力工作,看起來我遇到了障礙。如何在Maps API v3中將InfoWindow和Bounce Animation添加到多個標記?

事情是,我在地圖上有多個標記,我希望它們對每個標記都有不同的InfoWindows,並在點擊時向它們添加「跳動動畫」。

我的代碼目前是:

<script type="text/javascript"> 
    function initialize() { 
    var myLatlng = new google.maps.LatLng(-23.425553, -51.9382952); 
    var Deville = new google.maps.LatLng(-23.4253651, -51.9363681); 
    var Indaia = new google.maps.LatLng(-23.420335, -51.9415658); 
    var Elo = new google.maps.LatLng(-23.4249708, -51.9400302); 
    var Bristol = new google.maps.LatLng(-23.4236666, -51.9370105); 
    var Aeroporto = new google.maps.LatLng(-23.4796959, -52.0121323); 
    var Rodoviaria = new google.maps.LatLng(-23.4255414, -51.910845); 
    var CasaEmilia = new google.maps.LatLng(-23.4246251, -51.9405511); 
    var CentroComercial = new google.maps.LatLng(-23.4255029, -51.9461872); 
    var Binho = new google.maps.LatLng(-23.4227188, -51.9438343); 
    var CasaUniverso = new google.maps.LatLng(-23.4232428, -51.93649); 
    var Divinita = new google.maps.LatLng(-23.4240644, -51.9363321); 
    var CasaNoivos = new google.maps.LatLng(-23.4287239, -51.9328168); 
    var Hipico = new google.maps.LatLng(-23.4227585, -51.9688396); 


    var myOptions = { 
     center: myLatlng, 
     zoom: 16, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 

    var Deville = new google.maps.Marker({ 
     position: Deville, 
     map: map, 
     title:"Hotel Deville", 
     animation: google.maps.Animation.DROP, 
     }); 
    var Indaia = new google.maps.Marker({ 
     position: Indaia, 
     map: map, 
     title:"Hotel Indaiá", 
     animation: google.maps.Animation.DROP, 
     }); 
    var Elo = new google.maps.Marker({ 
     position: Elo, 
     map: map, 
     title:"Hotel Elo", 
     animation: google.maps.Animation.DROP, 
     }); 
    var Bristol = new google.maps.Marker({ 
     position: Bristol, 
     map: map, 
     title:"Hotel Bristol", 
     animation: google.maps.Animation.DROP, 
     }); 
    var Aeroporto = new google.maps.Marker({ 
     position: Aeroporto, 
     map: map, 
     title:"Aeroporto Regional de Maringá", 
     animation: google.maps.Animation.DROP, 
     }); 
    var Rodoviaria = new google.maps.Marker({ 
     position: Rodoviaria, 
     map: map, 
     title:"Rodoviária de Maringá", 
     animation: google.maps.Animation.DROP, 
     }); 
    var CasaEmilia = new google.maps.Marker({ 
     position: CasaEmilia, 
     map: map, 
     title: "Casa da Mãe do Noivo", 
     animation: google.maps.Animation.DROP, 
     }); 
    var CentroComercial = new google.maps.Marker({ 
     position: CentroComercial, 
     map: map, 
     title:"Centro Comercial Tiradentes", 
     animation: google.maps.Animation.DROP, 
     }); 
    var Binho = new google.maps.Marker({ 
     position: Binho, 
     map: map, 
     title:"Binho Importados", 
     animation: google.maps.Animation.DROP, 
     }); 
    var CasaUniverso = new google.maps.Marker({ 
     position: CasaUniverso, 
     map: map, 
     title:"Casa Universo", 
     animation: google.maps.Animation.DROP, 
     }); 
    var Divinita = new google.maps.Marker({ 
     position: Divinita, 
     map: map, 
     title:"Divinità Presentes", 
     animation: google.maps.Animation.DROP, 
     }); 
    var CasaNoivos = new google.maps.Marker({ 
     position: CasaNoivos, 
     map: map, 
     title:"Casa dos Noivos", 
     animation: google.maps.Animation.DROP, 
     }); 
    var Hipico = new google.maps.Marker({ 
     position: Hipico, 
     map: map, 
     title:"Clube Hípico de Maringá", 
     animation: google.maps.Animation.DROP, 
     }); 

    new google.maps.event.addListener(CasaNoivos, 'click', toggleBounce); 


    function toggleBounce() { 

     if (CasaNoivos.getAnimation() != null) { 
     CasaNoivos.setAnimation(null); 
     } else { 
     CasaNoivos.setAnimation(google.maps.Animation.BOUNCE); 
     } 

     if (Hipico.getAnimation() != null) { 
     Hipico.setAnimation(null); 
     } else { 
     Hipico.setAnimation(google.maps.Animation.BOUNCE); 
     } 
     } 
    } 
</script> 

我沒有添加彈跳的CasaNoivos的盯防,但是當我試圖添加到Hipico標記它被連接到Casanoivos兩者開始反彈,我想它是獨立。 我試圖用下面的代碼信息窗口和慘敗笑

 var infoDivinita = new google.maps.InfoWindow({ 
    content:'<div id="content">'+ 
      '<div id="siteNotice">'+ 
      '</div>'+ 
      '<h2 id="firstHeading" class="firstHeading">Divinità Presentes</h2>'+ 
      '<div id="bodyContent">'+ 
      '<p><b>Telefone:</b> (44) 3226-7449' + 
      '<b>Endereço:</b> Av. XV de Novembro, 351, Centro. CEP: 87013-230, Maringá-PR'+ 
      '<b>Site:</b> http://divinitapresentes.com.br </p>' 
      '</div>'+ 
      '</div>';' 
      }) 
    google.maps.event.addListener(Divinita, 'click', function() { 
    infoDivinita.open(map,Divinita); 
    }); 

任何幫助是極大的讚賞。

回答

2

反彈發生在一起,因爲它們都在相同的功能測試。你需要調用單個標記的功能,例如

new google.maps.event.addListener(CasaNoivos, 'click', function(e) {toggleBounce(CasaNoivos)}); 
    new google.maps.event.addListener(Hipico, 'click', function(e) {toggleBounce(Hipico)}); 

    function toggleBounce(marker) { 

     if (marker.getAnimation() != null) { 
     marker.setAnimation(null); 
     } else { 
     marker.setAnimation(google.maps.Animation.BOUNCE); 
     } 
    } 

關於您的信息窗口,我發現了一些語法錯誤,如「;」最後。

var infoHipico = new google.maps.InfoWindow({ 
content:'<div id="content">'+ 
     '<div id="siteNotice">'+ 
     '</div>'+ 
     '<h2 id="firstHeading" class="firstHeading">Hipico</h2>'+ 
     '<div id="bodyContent">'+ 
     '<p><b>Telefone:</b> (44) 3226-7449' + 
     '<b>Endereço:</b> Av. XV de Novembro, 351, Centro. CEP: 87013-230, Maringá-PR'+ 
     '<b>Site:</b> </p>'+ 
     '</div>'+ 
     '</div>' 
     }) 
google.maps.event.addListener(Hipico, 'click', function() { 
    closeInfoWindows(); // this function makes sure only one infowindow stays open 
    infoHipico.open(map,Hipico); 
}); 

var infoCasaNoivos = new google.maps.InfoWindow({ 
content:'<div id="content">'+ 
     '<div id="siteNotice">'+ 
     '</div>'+ 
     '<h2 id="firstHeading" class="firstHeading">Casa Noivos</h2>'+ 
     '<div id="bodyContent">'+ 
     '<p><b>Telefone:</b> (44) 3226-7449' + 
     '<b>Endereço:</b> Av. XV de Novembro, 351, Centro. CEP: 87013-230, Maringá-PR'+ 
     '<b>Site:</b> </p>'+ 
     '</div>'+ 
     '</div>' 
     }) 
google.maps.event.addListener(CasaNoivos, 'click', function() { 
    closeInfoWindows(); // this function makes sure only one infowindow stays open 
    infoCasaNoivos.open(map,CasaNoivos); 
}); 

    function closeInfoWindows() { 
    infoHipico.close(); 
    infoCasaNoivos.close(); 
    } 

最後,你應該使用數組。它簡化了一些事情,例如,如果您需要修復其中一個InfoWindow創建塊。

相關問題