我必須在我的網站上顯示谷歌地圖標記,其中緯度和經度的值來自latitude
和longitude
列的數據庫。使用php設置從數據庫到JavaScript的值
最初的JavaScript代碼是
var map;
function initialize() {
map = new GMaps({
div: '#map',
lat: -37.817917,
lng: 144.965065,
zoom: 16
});
map.addMarker({
lat: -37.81792,
lng: 144.96506,
title: 'Marker with InfoWindow',
icon: 'images/map-marker.png',
infoWindow: {
content: '<p>Melbourne Victoria, 300, Australia</p>'
}
});
}
我試圖把PHP代碼爲:
lat: <?php echo $latitude; ?>
lng: <?php echo $latitude; ?>
但它不工作。
我想出這樣做另一種方式是設置輸入字段值的座標,然後調用JavaScript的使用getElementById
<input id="map_latitude" value="<?= $latitude ?>" hidden>
<input id="map_longitude" value="<?= $longitude ?>" hidden>
,並在JavaScript:(這僅僅是實驗性的,不知道是否正確與否)
lat: getElementById('#map_latitude'),
lng: getElementById('#map_longitude')
第二部分要工作。嘗試使'lat:$('#map_latitude')。val(), lng:。$(#map_longitude')。val()'。如果你設置JQuery。 PS:確保你沒有得到空值。 – KubiRoazhon
[如何將變量和數據從PHP傳遞到JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Hypaethral