需求:從本地數據庫讀取經緯度,通過百度API在地圖上打點顯示(最簡示例)
開發環境:PHP+Mysql
源碼:
1. bm.html 代碼:
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微軟雅黑";}
#l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
#r-result{height:100%;width:20%;float:left;}
添加多個標注點//將字符串轉為JSon
function str2JSON(str) {
str=$.trim(str);
if(str!=""){
return eval('(' + str + ')');
}else{
return Object();
}
}
//通過jquery的Ajax獲取Mysql中的經緯度
function get_trj(fn){
var url="setPoint.php"
$.get(url,{
method:'get_trj'
},function(result){//result為php返回的JSON字符串
result=str2JSON(result);
// alert(result);
fn(result); //這是回調函數,調用下面的 trj_callback 變量中方法,不懂得話,百度JQuery Ajax
});
}
// 百度地圖API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.404, 39.915);//坐標中心是天安門
map.centerAndZoom(point, 15);
// 編寫自定義函數,創建標注
function addMarker(point){
var marker = new BMap.Marker(point);
map.addOverlay(marker);
}
var trj_callback=function(pts_array){
//將獲取的array轉換為BMap.Point
var sContentArray=Array();
var infoWindowArray=Array();
for(var i=0;i
//這是在地圖上打點,往里面傳個經緯度即可,自己完善下循環就可以了
addMarker(new BMap.Point(pts_array[i][0],pts_array[i][1]));
}
}
//這是程序的入口,調用,
get_trj(trj_callback);
2. setPoint.php 代碼:
/**
* 數據庫配置信息
*/
$db_config = array(
'host' => "localhost",
'user' => "root",
//'password' => "password",
'password' => "root",
//'database' => "gis"
'database' => "map"
);
/**
* flush outputbuffer並將其輸出到客戶端,可實現即時輸出
* @param unknown_type $str
*/
function qk_echo($str) {
echo $str;
ob_flush();//刷新緩存
flush();
}
/**
* Helper 類,用於接收http請求並對外提供服務
*/
class Helper{
private $db_config;
public function __construct($db_config){
$this->db_config=$db_config;
}
/**
* 對外提供api接口
* @param method api名稱
*/
public function do_api(){
$method=$_GET['method'];
$result="";
switch ($method) {
case 'get_trj':
# code...
$result=$this->get_trj();
break;
default:
# code...
break;
}
qk_echo($result);//Response
}
/**
* 獲取軌跡信息,以字符串的形式返回
*/
public function get_trj(){
//初始化數據庫連接
$conn=mysql_connect($this->db_config['host'], $this->db_config['user'],$this->db_config['password']);
//選擇數據庫
@mysql_select_db($this->db_config['database'],$conn);
//$table_name="trajectory";
//$table_name="ap_max";
$table_name="wifi_state";
//$sql="select id,lng,lat,ssid,rssiMax,rssiMin,wifi_mac,testPointNum,wifi_mac_used from ".$table_name;
//$sql="select id,lon,lat,time,encyptiontype,rssi,name from ".$table_name;
$sql="select id,lon,lat,time,encyptiontype,rssi,name from wifi_state";
// 執行sql查詢
$rs=mysql_query($sql,$conn);
$result=array();
while($row=mysql_fetch_row($rs)){
//加入經緯度信息
//echo $row[1];
$result[]=array($row[1],$row[2],$row[3],$row[4],$row[5],$row[6]);
//$result[]=array($row[1],$row[2],$row[3],$row[4]);
}
//關閉數據集
@mysql_close($rs);
//返回結果
return json_encode($result);
}
}
/*$link=mysql_connect("localhost","root","root");
if(!$link) echo "FAILD!連接錯誤,用戶名密碼不對";
else echo "OK!可以連接"; */
$helper=new Helper($db_config);
$helper->do_api();
$helper->get_trj();
3. MySQL數據庫:
數據庫名:map
表名:wifi_state
導出文件(可直接導入Mysql):
-- phpMyAdmin SQL Dump
-- version phpStudy 2014
-- http://www.phpmyadmin.net
--
-- 主機: localhost
-- 生成日期: 2017 年 03 月 15 日 08:37
-- 服務器版本: 5.5.53
-- PHP 版本: 5.4.45
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- 數據庫: `map`
--
CREATE DATABASE `map` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `map`;
-- --------------------------------------------------------
--
-- 表的結構 `wifi_state`
--
CREATE TABLE IF NOT EXISTS `wifi_state` (
`id` int(10) NOT NULL,
`time` date NOT NULL,
`lat` varchar(123) NOT NULL,
`lon` varchar(123) NOT NULL,
`name` varchar(123) NOT NULL,
`encyptiontype` varchar(123) NOT NULL,
`rssi` varchar(123) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- 轉存表中的數據 `wifi_state`
--
INSERT INTO `wifi_state` (`id`, `time`, `lat`, `lon`, `name`, `encyptiontype`, `rssi`) VALUES
(1, '2017-03-08', '39.915', '116.408', 'netgear41', 'unlocked', '0'),
(1, '2017-03-08', '39.910', '116.401', 'netgear41', 'unlocked', '0'),
(1, '2017-03-08', '39.917', '116.406', 'netgear41', 'unlocked', '0'),
(1, '2017-03-08', '39.912', '116.408', 'netgear41', 'unlocked', '0'),
(1, '2017-03-08', '39.910', '116.407', 'netgear41', 'unlocked', '0');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
4.完成效果示例:
5. 完整項目源碼下載地址:點擊下載