Table of Contents
回复内容:
Home Backend Development PHP Tutorial php做附近的人,根据距离由近到远进行排序

php做附近的人,根据距离由近到远进行排序

Jun 06, 2016 pm 08:06 PM
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>
Copy after login

这个范围是一个粗略的范围,后面距离计算后把超过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>
Copy after login

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>
Copy after login

}
}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles