Blogger Information
Blog 4
fans 0
comment 0
visits 5600
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
纯数据库做离我最近的排序
蓝枫的博客
Original
1019 people have browsed it

最近在做一个手机的团购网站,其中有个功能是‘离我最近’,这功能牵扯到两点,

第一,要获取用户当前的位置;第二,要把当前位置代入mysql中计算距离来排序;

实现一:

<script>
getLocation();
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else{alert(“该浏览器不支持获取地理位置。”);}
}
//获取经纬度
function showPosition(position)
{
document.cookie=”x1=”+position.coords.latitude;
document.cookie=”y1=”+position.coords.longitude;
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
err=”用户拒绝对获取地理位置的请求。”
break;
case error.POSITION_UNAVAILABLE:
err=”位置信息是不可用的。”
break;
case error.TIMEOUT:
err=”请求用户地理位置超时。”
break;
case error.UNKNOWN_ERROR:
err=”未知错误。”
break;
}
alert(err);
}
</script>

用上面的代码,将会把当前用户的经纬度获取下来,并且保存到cookie中

x1为纬度,y1为经度;

实现问题二:

select *,round(6378.138*2*asin(sqrt(pow(sin( ({$x1}*pi()/180-ypoint*pi()/180)/2),2)+cos({$x1}*pi()/180)*cos(ypoint*pi()/180)* pow(sin( ({$y1}*pi()/180-xpoint*pi()/180)/2),2)))*1000) as distance from…….order by distance asc

其中,$x1为当前纬度,$y1为当前经度,xpoint为目标经度,ypoint为目标纬度;

这两个结合起来即可查询出最终排序。

标签:mysql html5 离我最近


转载请注明来源蓝普网络并以链接形式标明本文地址
本文链接: http://www.wbphp.cn/html/y05/8419.html


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments