i am using google map api v3 for javascript. i managed to get the map on my webpage. now i need to perform reverse geocoding when the user clicks on the map. for that i have written the necessary event listener and reverse geocoding code
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=My_Key&sensor=false"></script>
<script type="text/javascript">
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
}
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
var myLatLng = event.latLng;
var lat = myLatLng.lat();
var lng = myLatLng.lng();
var latlng = new google.maps.LatLng(lat, lng);
//Code to reverse geocode follows
geocoder.geocode({'latLng': latlng}, function( results, status ) {
if ( status == google.maps.GeocoderStatus.OK ) {
if ( results[1] ) {
map.setZoom( 11 );
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent( results[1].formatted_address );
infowindow.open( map, marker );
document.forms["wheregoing"]["start"].value=results[1].formatted_address;
}
} else {
alert("Geocoder failed due to: " + status);
}
});
});
function placeMarker( location ) {
var marker = new google.maps.Marker({
position: location,
map: map
});
map.setCenter(location);
}
The above mentioned code was for java script. in HTML i have declared a place where the map should be shown as follows
<div id="map_canvas"></div>
The problem i am facing is that when a user clicks on the map a placemarker should be placed at that point and the address should appear in a textbox named "start" in the html form, but it does not happen so. The Map is getting loaded on the website but it is not detecting the click event. I am working on a localhost. I am testing this feature of map on localhost. Please help. Tell me where i am going wrong and why its not detecting the click event. i am a beginner in this
<script>at bottom of<body>maybe.