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); });