1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
function initCarte(){
var geocoder;
geocoder = new google.maps.Geocoder();
var locations = [
['1110 Morges', 'morges','titre1', 1],
['François 5<br>1003 Lausanne', 'lausanne','titre2', 2],
['1007 Lausanne','vevey','titre3', 3]
];
var oMap, oMarker, oInfo;
var i, nb = locations.length;
// création de la carte
oMap = new google.maps.Map(document.getElementById("map-canvas"),{
'zoom' : 8,
'center' : new google.maps.LatLng( 46.535248,6.632538),
'mapTypeId' : google.maps.MapTypeId.ROADMAP
});
// création des markers
for( i = 0; i < nb; i++){
// création marker
geocoder.geocode( { 'address': locations[i][1]}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
oMarker = new google.maps.Marker({
'numero' : i, // ici on sauve la valeur de i
'position' : results[0].geometry.location,
'map' : oMap,
'title' : locations[i][2]
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
// événement clic sur le marker
oInfo = new google.maps.InfoWindow();
google.maps.event.addListener( oMarker, 'click', function() {
// affectation du contenu en utilisant this.numero
oInfo.setContent(locations[this.numero][0]);
// affichage InfoWindow
oInfo.open( this.getMap(), this);
});
}
} |
Partager