如何将php结果传递给谷歌地图

时间:2012-09-21 10:27:03

标签: php mysql google-maps google-maps-api-3

我正在使用这个PHP代码从MySQL数据库中检索lat和lon,结果值应作为输入提供给google map.so当我运行查询时输出应该转到谷歌地图。作为参数我该怎么做?

这是php代码。     

    mysql_select_db("theatdb", $con);

    $result = mysql_query("SELECT Lat,Lon FROM theaters
    WHERE theater_name='theater_name'");

    while($row = mysql_fetch_array($result))
      {
      echo $row['theater_name'];
      echo "<br />";
      }
    ?> 

这是谷歌地图代码

 function initialize() {
        var latlng = new google.maps.LatLng(12.91869,77.594051);
        var myOptions = {
          zoom: 12,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

php代码正在检索我希望将结果传递给google maps的lat和lon值。我可以这样做吗?有人帮忙...提前谢谢。

2 个答案:

答案 0 :(得分:1)

使用mysql_fetch_assoc()从数据库中获取关联数组,因为只得到一个结果:

<?php
    mysql_select_db("theatdb", $con);
    $result = mysql_query("SELECT Lat,Lon FROM theaters WHERE theater_name='theater_name'");
    $row = mysql_fetch_assoc($result);
    echo $row['theater_name'];
?>

<script type="text/javascript">
function initialize() {
    var latlng = new google.maps.LatLng(<?php echo $row['Lat'];?>,<?php echo $row['Lon'];?>);
    var myOptions = {
      zoom: 12,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
}
</script>

答案 1 :(得分:1)

让我知道这是你试图获得的,或者你指的是其他东西

var lat  = <?php echo $result['lat'] ?>; 
var lang  = <?php echo $result['lang'] ?>;
var latlng = new google.maps.LatLng(lat,lang);