2014-07-22 77 views
0

我有codeigniter項目調用控制器時,頁眉,頁腳加載。所以在標題被加載後,我想添加一個谷歌地圖來獲取地圖被點擊時的經度和緯度到文本框。目前我可以在codeigniter之外做到這一點。但是我不能在頭文件加載後在CI內執行此操作。我認爲它是因爲谷歌使用窗口加載函數來初始化地圖。谷歌地圖加載時,標題已被加載

<head> 
<!-- Some js files loaded here when controller construct --> 
</head> 
<body> 

<script src="https://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script> 
</head> 
<script type="text/javascript"> 
var map; 
function initialize() { 
var myLatlng = new google.maps.LatLng(40.713956,-74.006653); 

var myOptions = { 
    zoom: 8, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
} 

map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

var marker = new google.maps.Marker({ 
    draggable: true, 
    position: myLatlng, 
    map: map, 
    title: "Your location" 
}); 

google.maps.event.addListener(map,'click',function(event) { 
    document.getElementById("latbox").value = event.latLng.lat(); 
    document.getElementById("lngbox").value = event.latLng.lng(); 
}) 

google.maps.event.addListener(marker, 'click', function (event) { 
    document.getElementById("latbox").value = this.getPosition().lat(); 
    document.getElementById("lngbox").value = this.getPosition().lng(); 
}); 
} 
google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

<div id="map_canvas" style="width:50%; height:50%"></div> 

回答

0

如果你使用jQuery,你可以試試

$(document).ready(function(){ 
    // here paste google maps code 
}); 

地圖將初始化當DOM就緒

+0

我想這和它的工作謝謝 – Dushan