Retrieve latlong in Infowindow
by secretgspot
HTML
<html>
<head>
<title>
Adding markers and info windows on click and retrieving the latitude-longitude co-ordinates in the info window
</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
</script>
</head>
<body onload="initialize()">
<div id="map" style="width: 530px; height: 230px">
</div>
</body>
</html>
JavaScript
var map;
function initialize()
{
var myLatlng = new google.maps.LatLng(28.635157,77.22496);
map = new google.maps.Map(document.getElementById("map"),
{
zoom: 14,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
google.maps.event.addListener(map, 'click', function(event)
{
placeMarker(event.latLng);
});
}
function placeMarker(location)
{
var marker = new google.maps.Marker(
{
position: location,
map: map
});
var coords = location.lat().toFixed(6) + ', ' + location.lng().toFixed(6);
var infowindow = new google.maps.InfoWindow(
{
content: 'The latitude longitude co-ordinates are: ' + coords
});
google.maps.event.addListener(marker, 'click', function()
{
infowindow.open(map,marker);
});
}