php做附近的人,根据距离由近到远进行排序
用户登陆的时候会获取到该用户的经纬度,数据库中存有所有用户的经纬度,如何进行由近到远进行排序,并算出距离
回复内容:
用户登陆的时候会获取到该用户的经纬度,数据库中存有所有用户的经纬度,如何进行由近到远进行排序,并算出距离
可以考虑用GeoHASH实现,效率更高,参考这篇http://www.cnblogs.com/LBSer/p/3310455.html
我觉得是时候把这个地址贴上来了。
https://segmentfault.com/q/1010000000345194
用php+redis可以实现 可以参考这个 http://www.wubiao.info/401
PHP安装GeoIP扩展和数据库根据IP获取访客所在国家/城市/经纬度等信息
然后就可以用geoip_record_by_name($_SERVER['REMOTE_ADDR'])
获取用户经纬度.
注意:geoip_record_by_name()返回的西经和南纬是负数.
5000米转成经纬度:
纬度 Latitude: 1 deg = 110852 m
经度 Longitude: 1 deg = 111320*cos(lat) m
同一经线上,相差一纬度约为 110852 米
同一纬线上,相差一经度约为 111320*cos(lat) 米 (lat为该纬线的纬度)
<code><?php //以当前用户经纬度为中心,查询5000米内的其他用户 $y = 5000 / 110852; //纬度的范围 $x = 5000 / (111320*cos($lat)); //经度的范围 $sql = ' select * from user where lat > ($lat-$y) and lat ($lon-$x) and lon </code>
这个范围是一个粗略的范围,后面距离计算后把超过5公里的用户筛掉即可.
根据上面查询出来的用户的经纬度,
用半正矢公式根据经纬度计算两点间距离:
<code><?php function distance($lat1, $lon1, $lat2, $lon2) { $R = 6371393; //地球平均半径,单位米 $dlat = deg2rad($lat2-$lat1); $dlon = deg2rad($lon2-$lon1); $a = pow(sin($dlat/2), 2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * pow(sin($dlon/2), 2); $c = 2 * atan2(sqrt($a), sqrt(1-$a)); $d = $R * $c; return round($d); } echo distance(0, 0, -1, 0); // 111202米</code></code>
elasticsearch 得距离搜索,获取最近的20公里数据
GET test/test/_search
{
"query": {
<code>"filtered": { "query" : { "match_all" : {} }, "filter": { "geo_distance": { "distance": 20, "distance_unit": "km", "location": { "lat" : 40.11116, "lon" : -71.34115 } } } }</code>
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
