我試圖使用Google Maps V3 API在Google地圖上創建標記。我在mySQL數據庫中有標記的座標,並且當前位於我的.php文件中的PHP數組中。現在我該如何使用foreach()循環(或其他合適的方法)遍歷我的PHP數組中的元素,並在循環的每次迭代中創建一個新的Google Map標記?JavaScript中使用PHP的foreach循環(Googlel Maps API)的MySQL數據
PS:我的PHP很體面,但不是我的javscript知識。現在我下面就創建標記的教程是http://www.svennerberg.com/2009/07/google-maps-api-3-markers/
代碼
我用笨的框架,因此控制器+模型文件已經獲取必要的數據(名稱,經度,緯度。 ..)到數組$ map中。我可以循環使用通常的方法在陣列:
foreach($map as $row) {
$lng = $row[lng] // this is not necessary, its just to show what is in the array
$lat = $row[lat]
// now how do I use this loop to create new marker code in javascript?
}
用於創建具有每foreach循環迭代中創建一次一個谷歌地圖標記的JS代碼是這樣的:
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: new google.maps.LatLng($lng, $lat), // how do i pass the PHP variables into the JS code?
mapTypeId: google.maps.MapTypeId.ROADMAP
});
所以我的問題是我如何通過PHP數組中的PHP變量來創建上面的JS代碼,每個foreach()循環迭代一次?
顯示一些代碼,你有這麼遠,所以我們可以幫你 – Ibu
增加了一些代碼到原來的職位... – Nyxynyxx