更新:尝试在PHP中存储数组,然后调用一个javascript函数

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

}

});

}

回答:

看是否有此产生的结果你期待:

PHP:

<?php 

// ......

$address = array();

while ($sec = mysql_fetch_array($result2)) $address[++$x]= "$sec[5] $sec[7]";

// this should print the same data as you were getting before if you un-comment it

// print_r($address);

mysql_close($link);

?>

<div id="address_container"><?php print array_shift($address); ?></div>

</div> <!-- I left this here because I don't know what the rest of your HTML looks like... -->

<script type="text/javascript">

document.write(codeAddress());

</script>

的Javascript:

function codeAddress() { 

var address = document.getElementByID("address_container").innerHTML;

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: " + status);

}

});

}

回答:

不print_r的$地址,但呼应它变成一个JS变种

</div> 

<script type="text/javascript">

var address = <?php echo json_encode($address); ?> ;

document.write(codeAddress());

</script>

那么没有必要在你的函数来选择它

回答:

这是什么例子我正要离开...从谷歌api

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"), myOptions);

}

function codeAddress() {

var address = document.getElementById("address").value;

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: " + status);

}

});

}

<body onload="initialize()">

<div id="map_canvas" style="width: 320px; height: 480px;"></div>

<div>

<input id="address" type="textbox" value="Sydney, NSW">

<input type="button" value="Encode" onclick="codeAddress()">

</div>

</body>

ref:http://code.google.com/apis/maps/documentation/javascript/services.html#GeocodingAddressTypes

以上是 更新:尝试在PHP中存储数组,然后调用一个javascript函数 的全部内容, 来源链接: utcz.com/qa/261081.html

回到顶部