我試圖將結果存儲在使用PHP的MySQL數組中,然後使用結果調用JavaScript函數以在那裏使用結果。我不知道爲什麼我的地圖上沒有顯示出來(試圖實現谷歌地圖在我的網頁)更新:嘗試在PHP中存儲數組,然後調用一個javascript函數
我的PHP/HTML/JavaScript調用
<?php
.....
<div id="content"
......
$address=array();
//will list out where to go
while ($sec = mysql_fetch_array($result2)) {
$address[$x++] = ($sec[5] . " " . $sec[7]);
}
print_r($latlng);
print_r($address);
mysql_close($link);
?>
<div id="address_container">
<?php
print array_shift($address);
?>
</div>
</div>
我的Javascript代碼:
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOp$
}
function codeAddress() {
var address = document.getElementByID("address_container").innerHTML;
console.log(address);
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + s$
}
});
}
什麼是ID爲「address」的HTML元素?你想用什麼格式將數據從PHP傳輸到JavaScript?我想你需要使用/缺少對[json_encode()](http://php.net/manual/en/function.json-encode.php)的調用...因爲它是你傳入一個引用到HTML元素本身,這不可能是正確的?它可以?在谷歌地圖上不是很大,但我不能相信這是它期望的... – DaveRandom
我想輸入一個地址數組...例如。 Array([1] => 1251 Front Street Lahaina,HI 96761 [2] => 160 Keonekai Rd Hihei,HI 96753)。然後將其輸入到javascript中以將這些地址放入以在地圖上放置標記。但現在我的地圖也沒有出現... – pythono