更新:尝试在php中存储数组然后调用javascript函数

时间:2011-09-05 10:53:16

标签: php javascript

我正在尝试使用php将结果存储在mysql的数组中,然后使用结果调用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$
      }
    });
}

3 个答案:

答案 0 :(得分:1)

看看这是否会产生您期望的结果:

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);
    }
  });
}

答案 1 :(得分:0)

不要打印_r $地址,而是将其回显到js var

</div>
<script type="text/javascript">
var address = <?php echo json_encode($address); ?> ;
document.write(codeAddress());
</script>

然后无需在函数中选择它

答案 2 :(得分:0)

这就是我从谷歌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>

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