フィールドにGoogle APIマップの座標を入力するJavaScriptコードを書いています。ただし、問題は、Googleマップの初期化です
<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>
他のすべてのコードがロードされた後に発生します(私が理解しているように、私はjsを始めたばかりです)。
<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>
そのため、以下のどこかにフィールドを埋めるためにこのコードが置かれていますが、initialize(); の前にロードされます。そして、もう実行されなくなった後は?
どうすればこの問題を解決できますか?
編集:(私が省略していたイベントリスナーを追加しました)
google.maps.event.addListener(map, 'bounds_changed', function() { window.setTimeout(function() {
map.panTo(marker.getPosition());
map.setZoom(4);
getBounds(map,currentv);
}, 3000); });