Git Product home page Git Product logo

laravel_geohash's Introduction

laravel_geohash

在laravel中使用geohash实现附近的功能

随着附近的X ,越来越实用。很多APP都加入了该功能,那么它该怎么实现?在php中如何使用。首先可以参考:

http://blog.dixo.net/downloads/geohash-php-class/

环境

laravel 5.8

使用距离

假设,使用手机获取到经纬度 30.555, 104.07,然后需要查找附近的软件公司。

$lat = '30.555';
$long = '104.07';
$geohash = new GeoHash();

$hash = $geohash->encode($lat, $long);
// 决定查询范围,值越大,获取的范围越小
// 当geohash base32编码长度为8时,精度在19米左右,而当编码长度为9时,精度在2米左右,编码长度需要根据数据情况进行选择。
$pre_hash = substr($hash, 0, 5);

//取出相邻八个区域
$neighbors = $geohash->neighbors($pre_hash);
array_push($neighbors, $pre_hash);

$values = '';
foreach ($neighbors as $key=>$val) {
  $values .= '\'' . $val . '\'' .',';
}
$values = substr($values, 0, -1);

通过以上操作,会获取到:$values = "'wm6n0','wm6j8','wm6jc','wm3vz','wm3yp','wm6n1','wm6j9','wm3vx','wm6jb'";

拼接成这样的字符串主要是为了后面的数据库查询工作。

用sql查询操作:

SELECT * FROM `address` WHERE LEFT(`geohash`,5) IN ('wm6n0','wm6j8','wm6jc','wm3vz','wm3yp','wm6n1','wm6j9','wm3vx','wm6jb')

查找出来,范围是: 这里写图片描述

到这里,从数据库返回的结果,就是你附近的店,但是这时候并没有计算出实际距离与排序,还需要写代码计算实际距离并排序。

备注:这只是附近的点实现的一种技术方案。更推荐使用mongodb

其他资料:

laravel_geohash's People

Contributors

helei112g avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.