使用ajax从MySql读数据,根据表中的经纬度信息在地图上打点

本文介绍了如何使用PHP从MySQL数据库中获取经纬度信息,并通过Ajax技术在地图上进行实时标注展示。详细讲解了PHP文件的代码实现过程。
摘要由CSDN通过智能技术生成

php文件代码如下:

<?php

/**
 * 数据库配置信息
 */
$db_config = array(
		'host' => "localhost", 
		'user' => "root", 
		//'password' => "password", 
		'password' => null,
		//'database' => "gis"
		'database' => "a0722152915"
);
/**
 * 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
	}

	/**
	 * 获取轨迹信息,以字符串的形式返回
	 */
	private 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";
		//$table_name="wifi";
		//$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,gps_lon,gps_lat,wifi_name,ap_free from ".$table_name;
		

		// 执行sql查询
		$rs=mysql_query($sql,$conn);
    	$result=array();
    	while($row=mysql_fetch_row($rs)){
    		//加入经纬度信息
    		$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);
	}
}



$helper=new Helper($db_config);
$helper->do_api();

html文件如下

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transition
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值