Google Map – Open an InfoWindow When Clicking on The Marker

Open an InfoWindow When Clicking on The Marker

Click on the marker to show an infowindow with some text. Here the code :

[php]

<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js">
</script>

<script>
var myCenter=new google.maps.LatLng(51.508742,-0.120850);

function initialize()
{
var mapProp = {
center:myCenter,
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

var marker=new google.maps.Marker({
position:myCenter,
});

marker.setMap(map);

var infowindow = new google.maps.InfoWindow({
content:"Hello World!"
});

google.maps.event.addListener(marker, ‘click’, function() {
infowindow.open(map,marker);
});
}

google.maps.event.addDomListener(window, ‘load’, initialize);
</script>
</head>

<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>

[/php]

Output :

 




Leave a Reply