正如您在下面的代碼中看到的,我從MySQL數據庫中獲取數據,但是當我想要使用提取的「位置」設置「位置」谷歌地圖上的標記。在JavaScript中設置變量的值
<head>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxxxxxx&sensor=true"></script>
<script>
$.getJSON('http://www.wawhost.com/appProject/fetchmarker.php?callback=?', function(data) {
for (var i = 0; i < data.length; i++) {
localStorage.loc = data[i].location;
}
});
</script>
<script>
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var marker = new google.maps.Marker({
position: 'new google.maps.LatLng(' + localStorage.loc + ')',
map: map,
title: 'Hello World!'
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
</body>
謝謝!
調用'初始化()'你的JSON加載之後。 – Blender