Note that there are some explanatory texts on larger screens.

plurals
  1. POIs JavaScript Closure the Solution here, How to use it?
    text
    copied!<p>I have google maps implemented and I am getting latitude and longitude, I want thse values to be used outside of the google maps function. I can add them to the DOM only when I call these variables within the google maps code. How can I use these variables outside to google maps function.</p> <p>On click of the maps, I need to append these latitude and longitude values to the URL as a querystring.</p> <p>I am not sure how Closures work exactly. Can someone please help me adding these latitude and longitude values to the querystring outside of google maps initilaize() function.</p> <p>Below is my code;</p> <pre><code>&lt;script&gt; function initialize() { var mapOptions = { center: new google.maps.LatLng(44.397, -122.644), zoom: 4, mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); var marker = new google.maps.Marker({ position: new google.maps.LatLng(44.397, -122.644), map: map, draggable: true }); google.maps.event.addListener(map, 'mousemove', function ques(event) { var lat_value = event.latLng.lat(); var lon_value = event.latLng.lng(); document.getElementById("llat").value = lat_value; document.getElementById("llon").value = lon_value; }); google.maps.event.addListener(map, 'click', function cclick() { }) } &lt;/script&gt; &lt;script&gt; $(function () { // Access functions and variables defined in google maps initialize() function here if(document.location.href.indexOf('?') == -1) { window.location = window.location + "?lat_value"; } }); &lt;/script&gt; </code></pre> <p><a href="http://jsfiddle.net/VzF4Q/1/" rel="nofollow"><strong>FIDDLE</strong></a></p>
 

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