如何将 php 数组从 mysql 返回到谷歌地理位置

Posted

技术标签:

【中文标题】如何将 php 数组从 mysql 返回到谷歌地理位置【英文标题】:How to return php array from mysql into google geolocation 【发布时间】:2012-05-19 17:09:18 【问题描述】:

我在 mysql 数据库中有数据,我希望能够读取 php 数组中的数据并将其用于谷歌地理定位。

所以对于这个例子,我只想使用SELECT * 语句,因为我不会用一些参数来打扰你......

我想要实现的是从 A 点到 B 点的标记线(取决于保存 GPS 位置的位置)

这是我想要在我的地图上拥有的链接:POLYLINE 在此示例中,数组中只有 4 个数据我想要更多。

所以现在我们可以回到代码上来了。这是我连接到 mysql 的 PHP 脚本。我一直在使用 mysqli,因为我稍后会在数据库中存储过程,所以不要混淆。

class dbMySql 

static function Exec($query) 
// open database
$conn = mysqli_connect(
$GLOBALS['cfg_db_Server'],
$GLOBALS['cfg_db_User'],
$GLOBALS['cfg_db_Pass']
);
if($conn === false) 

    throw new Exception(mysqli_connect_error());

mysqli_select_db($conn,$GLOBALS['cfg_db_Name']);
$result = mysqli_query($conn,$query);
if(is_bool($result) && !$result) 

    $error = mysqli_error($conn);
    mysqli_close($conn);
    throw new Exception($error);

mysqli_close($conn);
return $result;


如何将此 php 脚本连接到 google API 页面上的示例代码,并在单击按钮而不是固定值时插入我的数组值:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=KEY&sensor=true">
    </script>
    <script type="text/javascript">
  function initialize() 
  var myLatLng = new google.maps.LatLng(0, -180);
  var myOptions = 
    zoom: 3,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  ;

  var map = new google.maps.Map(document.getElementById("map_canvas"),
      myOptions);
  var flightPlanCoordinates = [
    new google.maps.LatLng(37.772323, -122.214897),
    new google.maps.LatLng(21.291982, -157.821856),
    new google.maps.LatLng(-18.142599, 178.431),
    new google.maps.LatLng(-27.46758, 153.027892)
  ];
  var flightPath = new google.maps.Polyline(
    path: flightPlanCoordinates,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 2
  );

  flightPath.setMap(map);

    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:80%"></div>
    <div><button type="button">Click Me!</button></div>
  </body>
</html>

编辑:

这是我在执行您的代码时得到的:

EDIT2:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html  height: 100% 
      body  height: 100%; margin: 0; padding: 0 
      #map_canvas  height: 100% 
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=key&sensor=true">
    </script>
    <script type="text/javascript">
  function initialize() 
  var myLatLng = new google.maps.LatLng(0, 180);
  var myOptions = 
    zoom: 3,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  ;

  var map = new google.maps.Map(document.getElementById("map_canvas"),
      myOptions);
  var flightPlanCoordinates = [ <?php echo implode(',', $coordinates) ?> ];
  ];
  var flightPath = new google.maps.Polyline(
    path: flightPlanCoordinates,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 2
  );

  flightPath.setMap(map);

</script>
</script>
    <?PHP
class dbMySql 

    static function Exec($query) 
        // open database
        $conn = mysqli_connect('localhost','root','*****');
        if($conn === false) 
            throw new Exception(mysqli_connect_error());
    
        mysqli_select_db($conn,'data_gps');

    $result = mysqli_query($conn,$query);

        if(is_bool($result) && !$result) 
            $error = mysqli_error($conn);
            mysqli_close($conn);
            throw new Exception($error);
        

        mysqli_close($conn);

    return $result;
    

$coordinates  = array();
$result = dbMySql::Exec('SELECT lat,lng FROM data');
while ($row = mysqli_fetch_assoc($result))
   $coordinates[] = 'new google.maps.LatLng(' . $row['lat'] . ', ' . $row['lng'] . ')' ;
 ?>
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:80%"></div>
    <div><button type="button">Click Me!</button></div>
  </body>
</html>

【问题讨论】:

【参考方案1】:

我猜您需要在结果之间进行迭代,然后将它们回显到 javascript 中。

我假设您的数据库中存储了 lat 和 lng。

$coordinates  = array();
$result = dbMySql::Exec('SELECT lat, lng FROM table WHERE id = 1');
while ($row = mysqli_fetch_assoc($result))
    $coordinates[] = 'new google.maps.LatLng(' . $row['lat'] . ', ' . $row['lng'] . ')';

然后在你的 javascript 部分

var flightPlanCoordinates = [ <?php echo implode(',', $coordinates) ?> ];

编辑:

<!DOCTYPE html>
<?PHP
class dbMySql 

    static function Exec($query) 
        // open database
        $conn = mysqli_connect('localhost','root','*****');
        if($conn === false) 
            throw new Exception(mysqli_connect_error());
    
        mysqli_select_db($conn,'data_gps');

    $result = mysqli_query($conn,$query);

        if(is_bool($result) && !$result) 
            $error = mysqli_error($conn);
            mysqli_close($conn);
            throw new Exception($error);
        

        //mysqli_close($conn);

    return $result;
    

$coordinates  = array();
$result = dbMySql::Exec('SELECT lat,lng FROM data');
while ($row = mysqli_fetch_assoc($result))
   $coordinates[] = 'new google.maps.LatLng(' . $row['lat'] . ', ' . $row['lng'] . ')';
 ?>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html  height: 100% 
      body  height: 100%; margin: 0; padding: 0 
      #map_canvas  height: 100% 
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=key&sensor=true">
    </script>
    <script type="text/javascript">
  function initialize() 
  var myLatLng = new google.maps.LatLng(0, 180);
  var myOptions = 
    zoom: 3,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  ;

  var map = new google.maps.Map(document.getElementById("map_canvas"),
      myOptions);
  var flightPlanCoordinates = [<?php echo implode(',', $coordinates) ?>];
  var flightPath = new google.maps.Polyline(
    path: flightPlanCoordinates,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 2
  );

  flightPath.setMap(map);

</script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:80%"></div>
    <div><button type="button">Click Me!</button></div>
  </body>
</html>

【讨论】:

嗨,我已经尝试过你的代码。当我执行回波坐标时,我真的得到了经度和纬度的结果。但是当我把它放在我的代码中时它不起作用。地图未加载,我已将 php 放入脚本中 您的文件是否以 '.php' 结尾? 大家好,我需要你的帮助。检查这个问题:***.com/questions/10931109/…

以上是关于如何将 php 数组从 mysql 返回到谷歌地理位置的主要内容,如果未能解决你的问题,请参考以下文章

MySQL到谷歌图表PHP

如何使用谷歌应用引擎将 php 项目部署到谷歌云中?

使用php将文件备份到谷歌驱动器

从地址变量到谷歌地图[关闭]

如何将文件从 colab 或云存储复制到谷歌驱动器?

java中jpeg的地理定位