
如何快速實(shí)現(xiàn)REST API集成以優(yōu)化業(yè)務(wù)流程
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
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;}
</style>
<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你申請(qǐng)的百度地圖密鑰"></script
<title>添加多個(gè)標(biāo)注點(diǎn)</title>
</head>
<body>
<div id="allmap"></div>
</body>
<script type="text/javascript">
//將字符串轉(zhuǎn)為JSon
function str2JSON(str) {
str=$.trim(str);
if(str!=""){
return eval('(' + str + ')');
}else{
return Object();
}
}
//通過(guò)jquery的Ajax獲取Mysql中的經(jīng)緯度
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); //這是回調(diào)函數(shù),調(diào)用下面的 trj_callback 變量中方法,不懂得話,百度JQuery Ajax
});
}
// 百度地圖API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.404, 39.915);//坐標(biāo)中心是天安門
map.centerAndZoom(point, 15);
// 編寫自定義函數(shù),創(chuàng)建標(biāo)注
function addMarker(point){
var marker = new BMap.Marker(point);
map.addOverlay(marker);
}
var trj_callback=function(pts_array){
//將獲取的array轉(zhuǎn)換為BMap.Point
var sContentArray=Array();
var infoWindowArray=Array();
for(var i=0;i<pts_array.length;i++){
//這是在地圖上打點(diǎn),往里面?zhèn)鱾€(gè)經(jīng)緯度即可,自己完善下循環(huán)就可以了
addMarker(new BMap.Point(pts_array[i][0],pts_array[i][1]));
}
}
//這是程序的入口,調(diào)用,
get_trj(trj_callback);
</script>
</html>
<?php
/**
* 數(shù)據(jù)庫(kù)配置信息
*/
$db_config = array(
'host' => "localhost",
'user' => "root",
//'password' => "password",
'password' => "root",
//'database' => "gis"
'database' => "map"
);
/**
* flush outputbuffer并將其輸出到客戶端,可實(shí)現(xiàn)即時(shí)輸出
* @param unknown_type $str
*/
function qk_echo($str) {
echo $str;
ob_flush();//刷新緩存
flush();
}
/**
* Helper 類,用于接收http請(qǐng)求并對(duì)外提供服務(wù)
*/
class Helper{
private $db_config;
public function __construct($db_config){
$this->db_config=$db_config;
}
/**
* 對(duì)外提供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(){
//初始化數(shù)據(jù)庫(kù)連接
$conn=mysql_connect($this->db_config['host'], $this->db_config['user'],$this->db_config['password']);
//選擇數(shù)據(jù)庫(kù)
@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";
// 執(zhí)行sql查詢
$rs=mysql_query($sql,$conn);
$result=array();
while($row=mysql_fetch_row($rs)){
//加入經(jīng)緯度信息
//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]);
}
//關(guān)閉數(shù)據(jù)集
@mysql_close($rs);
//返回結(jié)果
return json_encode($result);
}
}
/*$link=mysql_connect("localhost","root","root");
if(!$link) echo "FAILD!連接錯(cuò)誤,用戶名密碼不對(duì)";
else echo "OK!可以連接"; */
$helper=new Helper($db_config);
$helper->do_api();
$helper->get_trj();
數(shù)據(jù)庫(kù)名:map
表名:wifi_state
導(dǎo)出文件(可直接導(dǎo)入Mysql):
-- phpMyAdmin SQL Dump
-- version phpStudy 2014
-- http://www.phpmyadmin.net
--
-- 主機(jī): localhost
-- 生成日期: 2017 年 03 月 15 日 08:37
-- 服務(wù)器版本: 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 */;
--
-- 數(shù)據(jù)庫(kù): map
--
CREATE DATABASE map
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE map
;
-- --------------------------------------------------------
--
-- 表的結(jié)構(gòu) 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;
--
-- 轉(zhuǎn)存表中的數(shù)據(jù) 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 */;
百度地圖取Mysql數(shù)據(jù)打點(diǎn)
鏈接: https://pan.baidu.com/s/1qSnSOtudYanU5Oi_cl28gA
提取碼: fn9s
文章轉(zhuǎn)自微信公眾號(hào)@PHP自學(xué)中心
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)