2013-01-14 7 views
4

je le code suivant qui montre un code postal des clients, et il marque sur la carte avec un marqueur:Afficher un ballon popup sur un marqueur de google maps

<?php if($_GET['postcode']){ ?> 

//alert(<?php echo $_GET['postcode'];?>) 

<?php }?> 

     function initialize() { 
     var mapOptions = { 
      zoom: 4, 
      center: new google.maps.LatLng("<?php echo $_GET['postcode'];?>"), 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     var map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions); 
     map.setTilt(45); 


     var addressArray = new Array("<?php echo $_GET['postcode'];?>"); 
     var geocoder = new google.maps.Geocoder(); 

     var markerBounds = new google.maps.LatLngBounds(); 

     for (var i = 0; i < addressArray.length; i++) { 
     geocoder.geocode({ 'address': addressArray[i]}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 

     var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location 
     }); 
     markerBounds.extend(results[0].geometry.location); 
     map.fitBounds(markerBounds); 

     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
     }); 
     } 
     } 

     function loadScript() { 
     var script = document.createElement('script'); 
     script.type = 'text/javascript'; 
     script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&' + 
      'callback=initialize'; 
     document.body.appendChild(script); 
     } 

     window.onload = loadScript; 
    </script> 

Ce que je dois quand le marqueur est cliqué, est une bulle popup dans laquelle les clients commandent des informations. Qu'est-ce que j'ai besoin d'ajouter à ce qui précède, pour afficher la bulle popup?

Répondre

5

Vérifiez la superposition InfoWindow: https://developers.google.com/maps/documentation/javascript/overlays#InfoWindows

fait à peu près ce que vous voulez, c'est, il affiche un ballon avec du contenu.

Ce que vous devez ajouter à votre code est le suivant:

Au début de votre script en tant que variable globale:

var infowindow; 

la carte initialisation api:

function initialize() { 
    infowindow = new google.maps.InfoWindow(); 

Après la création de la marque er:

var marker = new google.maps.Marker({ 
    map: map, 
    position: results[0].geometry.location 
}); 

google.maps.event.addListener(marker, 'click', function() { 
    infowindow.setContent("<p>Some HTML content</p>"); 
    infowindow.open(map,marker); 
}); 

Hope this vous aide

Questions connexes