Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First problem: You are adding the click listener to the map outside of the initialize function, so it is being added before the map is initialized:</p> <pre><code> &lt;script type="text/javascript"&gt; var geocoder; var map; var info_win; var pos; var marker; function initialize() { google.maps.visualRefresh = true; geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(-34.397, 150.644); var mapOptions = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); google.maps.event.addListener(map, 'click', function() { pos = map.getPosition(); map.setCenter(pos); geocoder.geocode({'location': pos}, function(results, status) { marker = new google.maps.Marker ({ map: map, position: pos }); info_win = new google.maps.InfoWindow ({ content: results[0].formatted_address, }); infowindow.open(map,marker); }); }); } &lt;/script&gt; </code></pre> <p>After that you will find that a <a href="https://developers.google.com/maps/documentation/javascript/reference#Map" rel="nofollow">google.maps.Map</a> object does not have a getPosition method. It does have a getCenter method, but you probably want to use the <a href="https://developers.google.com/maps/documentation/javascript/reference#MouseEvent" rel="nofollow">latLng property of the click event instead</a>.</p> <pre><code> google.maps.event.addListener(map, 'click', function(evt) { pos = evt.latLng; map.setCenter(pos); </code></pre> <p>And fix the code that opens the info_win to open the google.maps.InfoWindow that you created:</p> <pre><code> info_win = new google.maps.InfoWindow ({ content: results[0].formatted_address, }); info_win.open(map,marker); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload