SQL 语句:select location.* from (select *,round(6378.138*2*asin(sqrt(pow(sin( (36.668530*pi()/180-px_lat*pi()/180)/2),2)+cos(36.668530*pi()/180)*cos(px_lat*pi()/180)* pow(sin( (117.020359*pi()/180-px_lon*pi()/180)/2),2)))*1000) as distance from bsx_training where (px_state = 1) and (type_id != ) and (((px_lat = 27.683290277922) and (px_lat = 45.653769722078)) and ((px_lon = 105.81826766053) and (px_lon = 128.22245033947))) order by distance limit 0,10) location where (1=1) and (location.distance = 500)先忽略上面这条SQL语句。一一解释根据SQL排序的SQl语句
distance_sql(,,, = round(6378.138*2*asin(sqrt(pow(sin( ({}*pi()/180-{}*pi()/180)/2),2)+cos({}*pi()/180)*cos({}*pi()/180)* pow(sin( ({}*pi()/180-{}*pi()/180)/2),2)))*1000)
这是一个生成根据SQL排序函数代码
接下来下面是设置经纬度范围内的数据
(I( post.location = ( , ,I( post.location = [0 = [1 = getAround(,,1000000.= and (((px_lat = {[ minLat ]}) and (px_lat = {[ maxLat ]})) and ((px_lon = {[ minLng ]}) and (px_lon = {[ maxLng ]}))) (I( post.distance_sort = , .distance_sql(,, px_lon , px_lat ). as distance = distance (I( post.km = htmlspecialchars_decode(I( post.km ((, ) !== = ( , .= and (location.distance = {[1]}) ((, - ) !== = ( - , .= and ((location.distance = {[0]}) and (location.distance = {[1]})) ((, ) !== = ( , .= and (location.distance = {[1]})
下面算出经纬度范围内的数据控制函数
/** * @param $latitude 纬度 * @param $longitude 经度 * @param $raidus 半径范围(单位:米) * @return multitype:number */ function getAround($latitude,$longitude,$raidus)$PI = 3.14159265; $degree = (24901*1609)/360.0; $dpmLat = 1/$degree; $radiusLat = $dpmLat*$raidus; $minLat = $latitude - $radiusLat; $maxLat = $latitude + $radiusLat; $mpdLng = $degree*cos($latitude * ($PI/180)); $dpmLng = 1 / $mpdLng; $radiusLng = $dpmLng*$raidus; $minLng = $longitude - $radiusLng; $maxLng = $longitude + $radiusLng; return array (minLat= $minLat, maxLat= $maxLat, minLng= $minLng, maxLng= $maxLng);}
要实现根据经纬度排序
就直接调用distance_sql(lon1,lat1,lon2,lat2)传入参数 并且as 一个别名例如 as distance, 然后sql语句中 order by 排序 根据 distance排序
如果筛选距离段 1000米-2000米的数据
那就sql语句嵌套sql
select *.loation from (select *,round(6378.138*2*asin(sqrt(pow(sin( (36.668530*pi()/180-px_lat*pi()/180)/2),2)+cos(36.668530*pi()/180)*cos(px_lat*pi()/180)* pow(sin( (117.020359*pi()/180-px_lon*pi()/180)/2),2)))*1000) as distance) from table location where (location.distance = 1000) and (location.distance = 2000))
如果实现根据最近位置排序sql
select *,round(6378.138*2*asin(sqrt(pow(sin( (36.668530*pi()/180-px_lat*pi()/180)/2),2)+cos(36.668530*pi()/180)*cos(px_lat*pi()/180)* pow(sin( (117.020359*pi()/180-px_lon*pi()/180)/2),2)))*1000) as distance order by distance
public function training_list() $wheres1 = (px_state = 1) $wheres2 = where (1=1) $orderBy = px_id desc if(I( post.location )){ // 用户经纬度 $location = explode( , ,I( post.location )); $userLon = $location[0]; $userLat = $location[1]; // 经纬度筛选 $location = getAround($userLat,$userLon,1000000); $wheres1.= and (((px_lat = {$location[ minLat ]}) and (px_lat = {$location[ maxLat ]})) and ((px_lon = {$location[ minLng ]}) and (px_lon = {$location[ maxLng ]}))) // 经纬度距离筛选 if(I( post.distance_sort )){ $distanceSql = , .distance_sql($userLon,$userLat, px_lon , px_lat ). as distance $orderBy = distance } if(I( post.km )){ $kmStr = htmlspecialchars_decode(I( post.km )); if(strpos($kmStr, ) !== false){ $km = explode( ,$kmStr); $wheres2 .= and (location.distance = {$km[1]}) }else if(strpos($kmStr, - ) !== false){ $km = explode( - ,$kmStr); $wheres2 .= and ((location.distance = {$km[0]}) and (location.distance = {$km[1]})) }else if(strpos($kmStr, ) !== false){ $km = explode( ,$kmStr); $wheres2 .= and (location.distance = {$km[1]})
$field = *{$distanceSql} $sql = select location.* from (select {$field} from bsx_training where {$wheres1} order by {$orderBy} limit {$n},{$showNum}) location {$wheres2} $training = M()- query($sql);
独孤九贱(5)_ThinkPHP5视频教程ThinkPHP是国内最流行的中文PHP开发框架,也是您Web项目的最佳选择。《VeVb.com独孤九贱(5)-ThinkPHP5视频教程》课程以ThinkPHP5最新版本为例,从最基本的框架常识开始,将...
Peter-Zhu 2017-05-16 12:03:57
独孤九贱(4)_PHP视频教程江湖传言:PHP是世界上最好的编程语言。真的是这样吗?这个梗究竟是从哪来的?学会本课程,你就会明白了。PHP 出品的PHP入门系统教学视频,完全从初学者的角度出发,绝不玩虚的,一切以实用、有用...
Peter-Zhu 2017-03-20 22:47:17
独孤九贱(1)_HTML5视频教程《VeVb.com原创html5视频教程》课程特色:php 原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了HTML知识。...
Peter-Zhu 2017-03-13 10:15:11
ThinkPHP5实战之[教学管理系统]本套教程,以一个真实的学校教学管理系统为案例,手把手教会您如何在一张白纸上,从零开始,一步一步的用ThinkPHP5框架快速开发出一个商业项目。
Peter-Zhu 2017-07-24 16:48:56
PHP入门视频教程之一周学会PHP所有计算机语言的学习都要从基础开始,《PHP入门视频教程之一周学会PHP》不仅是PHP的基础部分更主要的是PHP语言的核心技术,是学习PHP必须掌握的内容,任何PHP项目的实现都离不开这部分的内容,通...
PHP教程郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答