php - 如何将 php 数组从 mysql 返回到 google geolocation

标签 php javascript html google-maps geolocation

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

因此,对于这个示例,我只想使用 SELECT * 语句,因为我不会用一些参数来打扰您...

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

这是我想要在 map 上显示的链接: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>

编辑:

这是我执行你的代码时得到的结果: error

编辑2:

<!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>

最佳答案

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

我假设您的数据库中存储了纬度和经度。

$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 数组从 mysql 返回到 google geolocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10667262/

相关文章:

javascript忽略方程元素大于1的if语句?

javascript - 使用method=post通过jquery提交表单

javascript - 将高度缩小到内容大小

PHP Firebase Clood Messaging (FCM) 错误缺少注册

PHP 邮件无限期挂起 - 如何调试?

php - jQuery .keyup() 函数

javascript - 使用 JavaScript 获取 2 个 DOM 元素之间的相对位置

PHP 显示每个 div 的一定数量的记录

javascript - 根据文件大小取消 node.js http.Client 上的文件下载/请求

javascript - 调用具有多个参数的函数