js 调用百度地图api,并在地图上进行打点,添加标注

最近要做一个网页,具体内容是:上边有一个标题,下边分成两块,左边是地图。并且地图上有两个点,点击两个点有相应的提示信息,显示数据库里最新的两条数据信息。右边是一些文字说明。本人刚开始学习,做的也不是很好

总体效果如下所示:


首先新建map.php文件,代码如下

[php]  view plain  copy
  1. <!DOCTYPE html>  
  2. <?php  
  3. /* 
  4. 创建与数据库的连接 
  5. */  
  6. $conn=mysql_connect("","",""or die("can not connect to server");  
  7. mysql_select_db("hdm0410292_db",$conn);  
  8. mysql_query("set names utf8");  
  9. //选择出两辆车插入的最新数据,并将两条语句存在数组里  
  10. $sql0="select * from car_info where carID='20140508'order by id desc limit 1";  
  11. $sql1="select * from car_info where carID= '20140510' order by id desc limit 1";  
  12. $sql=array($sql0,$sql1);  
  13. ?>  
  14. <html>  
  15. <head>  
  16. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  17. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  18. <title>车联网信息展示</title>  
  19. <style type="text/css">  
  20. html{  
  21.             height:99%}  
  22. body{     
  23.             height:99.9%;  
  24.             width:99%;  
  25.             font-family:楷体_GB2312;  
  26.             font-size:25px}  
  27. #container {height: 100%}  
  28. </style>  
  29. <script type="text/javascript"  
  30.     src="http://api.map.baidu.com/api?v=1.5&ak=你申请的秘钥"></script>  
  31. </head>  
  32. <body BGCOLOR="#CAFFFF">  
  33. <div id="container"></div>  
  34. <script type="text/javascript">  
  35.       
  36. var lon_center = 0;  
  37. var lat_center = 0;  
  38. var map = new BMap.Map("container");  
  39. <!-- 添加标注的函数,参数包括,点坐标,车ID,以及数据库里的其他信息-->  
  40. function addMarker(point,index,s){    
  41.     var fIcon = new BMap.Icon("car1.png"new BMap.Size(55, 43), {  
  42.              
  43.         });        
  44.     var sIcon = new BMap.Icon("car2.png"new BMap.Size(55, 43), {  
  45.           
  46.         });  
  47.     var myIcon = "";        
  48.         // 创建标注对象并添加到地图    
  49.     if(index == 20140508)  
  50.         myIcon=fIcon;  
  51.     else  
  52.         myIcon=sIcon;  
  53.     var marker = new BMap.Marker(point, {icon: myIcon});  
  54.     map.addOverlay(marker);   
  55.     marker.addEventListener("click",function(){  
  56.                 var opts={width:450,height:500,title:"详细信息"};  
  57.                 var infoWindow = new BMap.InfoWindow(s,opts);  
  58.                 map.openInfoWindow(infoWindow,point);   
  59.          });    
  60. }  
  61. <?php   
  62. //遍历数组里的两条sql语句  
  63. foreach ($sql as &$value) {  
  64.      $query=mysql_query($value);   
  65.      $row=mysql_fetch_array($query);       
  66. ?>     
  67. var lon= <?php echo $row[longitude] ?>;  
  68. var lat= <?php echo $row[latitude] ?>;  
  69. <!-- 计算两个点的中心点,并将其作为地图初始化时的中心位置-->  
  70. lon_center += lon;  
  71. lat_center += lat;  
  72. var id=<?php echo $row[id] ?>;  
  73. var info="<br/>"+"carID: " + "<?php echo $row[carID]?>" + " <br/> " +  
  74. "经度: " + "<?php echo $row[longitude]?>" + " <br/> " +   
  75. "纬度: " + "<?php echo $row[latitude]?>" + " <br/> " +    
  76. "速度: " + "<?php echo $row[speed]?>" + "Km/h" + " <br/> " +  
  77. "加速度: " + "<?php echo $row[acceleration]?>" + " <br/> " +  
  78. "方向: " + "<?php echo $row[direction]?>" + " <br/> " +  
  79. "油量: " +  "<?php echo $row[oil]?>" + "<br/>" +   
  80. "地址: " +  "<?php echo $row[street]?>";  
  81. var point = new BMap.Point(lon, lat);  
  82. addMarker(point,<?php echo $row[carID] ?>,info);  
  83. <?php   
  84. }  
  85. ?>  
  86. <!-- 计算两个点的中心点,并将其作为地图初始化时的中心位置-->    
  87. var center = new BMap.Point(lon_center/2,lat_center/2);   
  88. map.centerAndZoom(center, 17);  
  89. map.enableScrollWheelZoom();   
  90. </script>  
  91. </body>  
  92. </html>  
map.php文件主要是显示数据库里的两条信息,将这两条信息在地图上的相应的位置显示出来。

然后再建title.php,这个很简单,就是显示一个标题

[php]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6. <title>信息展示</title>  
  7. <style type="text/css">  
  8.   
  9. html{  
  10.             height:100%;}  
  11. body{     
  12.             height:10%;  
  13.             width:99%;  
  14.             font-family:楷体_GB2312;  
  15.             font-size:25px}  
  16. </style>  
  17. </head>  
  18. <body BGCOLOR="#CAFFFF">    
  19. <H1 ALIGN="CENTER"> 信息展示 </H1>  
  20. </body>  
  21. </html>  
然后在建立详细信息说明模块info.php

[php]  view plain  copy
  1. <!DOCTYPE html>  
  2. <?php  
  3.   
  4. $conn=mysql_connect("","",""or die("can not connect to server");  
  5. mysql_select_db("",$conn);  
  6. mysql_query("set names utf8");  
  7. $sql0="select * from car_info where carID='20140508'order by id desc limit 1";  
  8. $sql1="select * from car_info where carID= '20140510' order by id desc limit 1";  
  9. $sql=array($sql0,$sql1);  
  10.   
  11. function htmtocode($content){  
  12.     $content=str_replace("\n""<br>"str_replace(" "" "$content));  
  13.     return $content;  
  14. }  
  15. ?>  
  16. <html>  
  17. <head>  
  18. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  20. <title>信息展示</title>  
  21.   
  22. <style type="text/css">  
  23. html{  
  24.             height:90%;}  
  25. body{     
  26.             height:90%;  
  27.             width:90%;  
  28.             font-family:楷体_GB2312;  
  29.             font-size:20px}  
  30. </style>  
  31. </head>  
  32. <body BGCOLOR="#CAFFFF">    
  33. <H1 ALIGN="CENTER"> 信息展示 </H1>  
  34. <?php foreach ($sql as &$value) {  
  35.      $query=mysql_query($value);   
  36.      $row=mysql_fetch_array($query);  
  37. ?>  
  38. <H2>car <?php echo $row[carID]?> 详细信息</H2>  
  39. <HR>  
  40. CAR ID: <?php echo $row[carID]?><br>  
  41. 经度:  <?php echo $row[longitude]?> <br>  
  42. 纬度:  <?php echo $row[latitude]?> <br>   
  43. 速度:  <?php echo $row[speed]?> Km/h <br>  
  44. 加速度:  <?php echo $row[acceleration]?><br>   
  45. 方向:  <?php echo $row[direction]?> <br>  
  46. 油量:  <?php echo $row[oil]?><br>   
  47. 地址:  <?php echo $row[street]?><br>  
  48. 时间:  <?php echo $row[date]?>  
  49. <?php } ?>    
  50. </body>  
  51. </html>  

最后在写一个vanet.php文件,该文件主要是调用前三个文件

[php]  view plain  copy
  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head>  
  5. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  7. <title>信息展示</title>  
  8. </head>  
  9.   
  10. <FRAMESET ROWS="10%,90%" FRAMEBORDER=1 >  
  11. <FRAME SRC="title.php"></FRAME>   
  12. <FRAMESET COLS="70%,30%">  
  13.     <FRAME SRC="map.php">  
  14.     <FRAME SRC="info.php">  
  15.     </FRAMESET>     
  16. </FRAMESET>  
  17. </html>  

大功告成!

猜你喜欢

转载自blog.csdn.net/my_csdn2018/article/details/80532295