我试图将结果存储在使用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