0

I'm am writing javascript code to fill a field with the coordinates of a google api map. The problem is however that the initialization of the google map

<script type="text/javascript">

  var coord,mapOptions,map;
  function initialize() {
    coord = new google.maps.LatLng(-25.363882, 131.044922);
    mapOptions = { zoom: 4, center: coord , mapTypeId: google.maps.MapTypeId.ROADMAP};
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
 }

 function getBounds(map,currentv)
 {
   currentv[0] = map.getCenter().lat();
   currentv[1] = map.getCenter().lng();
   return currentv;
 }
 </script>

happens (as i understand it; i've just began with js) after all the other code has loaded.

<script>
  if (map != undefined){
    currentv = getBounds(map,currentv);
    console.log("Debug: lat "+currentv[0]);
    document.write('<input type="text" name="latitude" value="'+ currentv[0] +'" />');
  } 
</script>

So somewhere below sits this code to fill the field but it's loaded before the initialize(); and then after it's not run anymore?

How can i solve this problem?

edit: (added the event listener which i had left out)

google.maps.event.addListener(map, 'bounds_changed', function() { window.setTimeout(function() { 

map.panTo(marker.getPosition());
map.setZoom(4);
getBounds(map,currentv); 

}, 3000); });
Kara
  • 6,115
  • 16
  • 50
  • 57
Thomas
  • 1,678
  • 4
  • 24
  • 47
  • have a look at this http://stackoverflow.com/questions/832692/how-to-check-if-google-maps-is-fully-loaded – olly_uk Sep 06 '12 at 10:20
  • I got it working using current_v.elements["current_long"].value = 'Some Value'; where current_v is the name of the form and current_long the field i want to change the value of. Don't know if's its browser safe though or the best way to do this? – Thomas Sep 06 '12 at 11:04
  • @olly_uk: that i had already figured out :p i didn't post the code but have added it now – Thomas Sep 06 '12 at 11:06

0 Answers0