php请求返回GeoJSON格式的数据
<?php /*
* Following code will list all the products
*/ // array for JSON response
$response = array(); // include db connect class
require_once __DIR__ . '/db_connect.php'; // connecting to db
$db = new DB_CONNECT(); // get all products from products table
$result = mysql_query("SELECT * FROM fbteam") or die(mysql_error()); # Build GeoJSON feature collection array
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
); // check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node while ($row = mysql_fetch_array($result)) {
$feature = array(
'id' => $row['id'],
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
# Pass Longitude and Latitude Columns here
'coordinates' => array($row['lon'], $row['lat'])
)
);
# Add feature arrays to feature collection array
array_push($geojson['features'], $feature);
}
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
echo json_encode($geojson, JSON_NUMERIC_CHECK);
} else {
echo "no data";
}
mysql_close($con);
db_connect.php
<?php /**
* A class file to connect to database
*/
class DB_CONNECT { // constructor
function __construct() {
// connecting to database
$this->connect();
} // destructor
function __destruct() {
// closing db connection
$this->close();
} /**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php'; // Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error()); // Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error()); // returing connection cursor
return $con;
} /**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
} }
config中配置数据库连接的信息
<?php /*
* All database connection variables
*/ define('DB_USER', "root"); // db user
define('DB_PASSWORD', "mypassword"); // db password
define('DB_DATABASE', "mydatabase"); // database name
define('DB_SERVER', "localhost"); // db server
?>
php请求返回GeoJSON格式的数据的更多相关文章
- springmvc通过ajax异步请求返回json格式数据
jsp 首先创建index.jsp页面 <script type="text/javascript"> $(function () { $("#usernam ...
- 3.自定义返回json格式的数据给前台(自定义Controller类中的Json方法)
在mvc的项目中,我们前台做一些操作时,后台要返回一些结果给前台,这个时候我们就需要有一个状态来标识到底是什么类型的错误, 例如: 执行删除的时候,如果操作成功(1行受影响),我们需要返回状态为1并输 ...
- SSH返回Json格式的数据
在开发中我们经常遇到客户端和后台数据的交互,使用比较多的就是json格式了.在这里以简单的Demo总结两种ssh返回Json格式的数据 项目目录如下 主要是看 上图选择的部分 WebRoot里面就 ...
- WebAPI搭建(二) 让WebAPI 返回JSON格式的数据
在RestFul风格盛行的年代,对接接口大多数人会选择使用JSON,XML和JSON的对比传送(http://blog.csdn.net/liaomin416100569/article/detail ...
- 人人商城返回Json格式的数据
人人商城返回Json格式的数据 1.找到该插件对应的 core/mobile 路径 2.新建一个 api.php 文件 <?php header('Content-Type:applicatio ...
- Golang-Gin Response 统一返回restful格式的数据
目的: gin返回restful格式的数据,返回的200,201 的数据 也包括异常时的404/500等情况 直接调用即可 package response import ( "github ...
- 使用jQuery发送POST,Ajax请求返回JSON格式数据
问题: 使用jQuery POST提交数据到PHP文件, PHP返回的json_encode后的数组数据,但jQuery接收到的数据不能解析为JSON对象,而是字符串{"code" ...
- asp.net MVC控制器中返回JSON格式的数据时提示下载
Asp.net mvc在接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据.代码如下: 视图中js代码: $("# ...
- 用shp制作geoJson格式地图数据(shp convert to geoJson)
本文紧接前文,简单说明利用shp数据制作Echarts支持的geoJson格式的地图数据.本文以北京市通州区各镇的shp数据为例进行说明. 软件环境: ArcGIS 10.2 (ArcGIS 10.2 ...
随机推荐
- 原生node的header
首先引入http模块 获取http.ServerResponse对象的方式,1.http.createServer(function(req,res){}) 其中res是http.ServerResp ...
- Spring操作指南-针对JDBC配置声明式事务管理(基于注解)
- MyBatis基于注解的动态SQL——概览
- 制作自己的嵌入式Linux电脑_转
制作自己的嵌入式Linux电脑 http://os.51cto.com/art/201409/450334.htm 原文链接:http://blog.jobbole.com/75414/ 包含器件选择 ...
- ubuntu安装使用GitHub--PC端
按照这个步骤安装并且初步使用 按照这个程序配置git的ssh,在git push时不用输入密码
- 化繁为简,最简易的SqlDataSource配合Cache使用~
最近一个小项目用了比较方便傻瓜的DevExpress控件,实践中DevExpress的控件确实很方便. 在用ASPxGridView控件的时候,为了偷懒结合了SqlDataSource使用,但一直在大 ...
- Activity 横竖屏切换
前言 在开发中常要处理横竖屏切换,怎么处理先看生命周期 申明 Activity 横竖屏切换时需要回调两个函数 ,所以在此将这个两个函数暂时看成是Activity 横竖屏切换的生命周期的一部分,这两个函 ...
- linux启动时文件系统错误问题
linux开机启动时,文件系统错误(磁盘有坏轨或文件系统出现错误) 原因: 1.系统运行时,突然断电或不正常关机,导致文件系统错误(文件系统错误并不是硬件错误,而是软件数据的问题) ...
- windows shell api SHEmptyRecycleBin 清空回收站
HRESULT SHEmptyRecycleBin( HWND hwnd, LPCTSTR pszRootPath, DWORD dwFlags ); hwnd 父窗口句柄 pszRootPath 将 ...
- MS SQL 两种分页
------ row number ---------- ------ row number ---------- declare @pageSize int,@pageIndex int ; sel ...