post
简要描述:
- 附近餐厅
请求URL:
- Nearby/Near/getLocation``
请求方式:
- POST
参数:
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
lat | 是 | string | 纬度 |
lng | 是 | string | 经度 |
返回示例
{
"status": 0,
"data": [
{
articleId : "...",
title : "...",
shopTitle : "....",
imgUrl : "....",
bacImgUrl : "...",//商家店名图
article : "....",//商家简介
longitude : "...",
latitude : "...",
}
]
}
返回参数说明
参数名 | 类型 | 说明 |
---|---|---|
status | int | 请求状态 : 0:成功 , 1 : 失败 |
title | striing | 标题 |
shopTitle | string | 店名 |
article | string / html | 介绍或是文章内容 |
price | number | 人均消费 |
后端代码如下
<?php
// define(EARTH_RADIUS, 6371);//地球半径,平均半径为6371km
Class Near extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model(array('General'));
}
function getLocation()
{
//处理经纬度
$lat = $this->input->post('lat');
$lng = $this->input->post('lng');
$returnSquare = $this->returnSquarePoint($lat,$lng);
$test = array('lat >'=>0,'lat>'=>$returnSquare['right-bottom']['lat'],'
lat <'=>$returnSquare['left-top']['lat'],'lng>'=>$returnSquare['left-top']
['lng'],'lng<'=>$returnSquare['right-bottom']['lng']);
$location = $this->General->location('article',$test);
$squareShop = array();
$slide = array();
foreach ($location as $key => $value) {
//根据文章id,获取文章图片,转换名称,输出json
$slide = $this->General->query('article_slide',array('article_id'=>$value['id']));
foreach ($slide as $k => $v) {
if($v['article_id'] == $value['id'])
$squareShop[$key]['imgUrl'] = $v['article_slide'];
}
/*查询商家名称*/
$shop_name = $this->General->query('shop',array('shop_name'=>$value['shop_name']));
foreach ($shop_name as $shop => $img) {
if(empty($shop_name))
{
$squareShop[$key]['shop_img'] = $squareShop[$key]['imgUrl']; //如果为空,则使用文章图片代替
}else{
$squareShop[$key]['shop_img'] =$img['shop_img'];
}
}
$squareShop[$key]['shopTitle'] = $value['shop_name'];
$squareShop[$key]['articleId'] = $value['id'];
$squareShop[$key]['title'] = $value['article_title'];
$squareShop[$key]['article'] = $value['abstract'];
$squareShop[$key]['dates'] = date('Y年m月d日',$value['dates']);
$squareShop[$key]['latitude'] = $value['lat'];
$squareShop[$key]['longitude'] = $value['lng'];
}
echo json_encode(array('status'=>0,'data'=>$squareShop));
}
/**
*计算某个经纬度的周围某段距离的正方形的四个点
*
*@param lng float 经度
*@param lat float 纬度
*@param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米
*@return array 正方形的四个点的经纬度坐标
*/
function returnSquarePoint($lat,$lng){
/*
lng 用户传过来的 lng
lat 用户传过来的lat
*/
$earth = $this->config->item('EARTH_RADIUS');
$distance = 2;
$dlng = 2 * asin(sin($distance / (2 * $earth)) / cos(deg2rad($lat)));
$dlng = rad2deg($dlng);
$dlat = $distance/$earth;
$dlat = rad2deg($dlat);
return array(
'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
);
/*
$info_sql = "select id,locateinfo,lat,lng from `lbs_info` where lat<>0 and lat>{$squares['right-bottom']['lat']} and lat<{$squares['left-top']['lat']} and lng>{$squares['left-top']['lng']} and lng<{$squares['right-bottom']['lng']} ";
*/
}
}
实现界面如下