douxie1692 2014-01-25 11:16
浏览 45
已采纳

与DB连接

I did what you advised, map is displayed correctly, but have no markers. Maybe I've made some mistakes? Code:

`

mysql_connect("xxx", "xxx", "xxx") or die("Error connecting to database:  ".mysql_error());


mysql_select_db("xxx") or die(mysql_error());


$querz  = "SELECT * FROM markers";
$result = mysql_query($querz)
or die("Query failed");

while ($row = mysql_fetch_array($result)) {
    $lat = "$row[lat]";
    $lng = "$row[lng]";
}

 ?>


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
 <meta charset="iso-8859-2">
  <title>Simple icons</title>
    <style>
    html, body, #map-canvas {
    height: 600px;
    margin: 0px;
    padding: 0px
     }
   </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>




function initialize() {
  var mapOptions = {
    zoom: 8,
   center: new google.maps.LatLng(52, 19)
  }
  var map = new google.maps.Map(document.getElementById('map-canvas'),
                            mapOptions);


   var image = '20_blue.png';

  var myLatLng = new google.maps.LatLng(<?php print($lat); ?>,<?php print($lng); ?>);
   var beachMarker = new google.maps.Marker({
  position: myLatLng,
  map: map,
  icon: image
  });
 }

google.maps.event.addDomListener(window, 'load', initialize);

  </script>
 </head>
 <body>
   <div id="map-canvas"></div>
 </body>
/html>`

my question: is there any way to display markers from Mysql database instead of use specific lat and lng , like I did in the code above ? How ? I tried google tutorials but it had some bugs (I' ve already asked another question about it).

  • 写回答

2条回答 默认 最新

  • duanqiang5722 2014-01-25 11:20
    关注

    You need a server side language to connect to your database (in your case I advise you to use PHP which is the simplest). You will be able to select your datas from the database with PDO.

    Edit:

    You need to write your php loop inside your javascript code. Here you are adding only one marker to your map, the last one you selected with your query. Also make sure the table is not empty.

    You can do something like that:

    <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          zoom: 8,
          center: new google.maps.LatLng(52, 19)
        }
    
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    
        var image = '20_blue.png';
    
        <?php while($row = mysql_fetch_array($result)): ?>
    
        var myLatLng = new google.maps.LatLng(<?php echo $row['lat']; ?>, <?php echo $row['lon']; ?>);
        var beachMarker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: image
        });
    
        <?php endwhile; ?>
      }
    
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP