分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

php LBS(附近地理位置)功能实现的一些思路

发布时间:2023-09-06 01:55责任编辑:熊小新关键词:暂无标签

在开发中经常会遇到把数据库已有经纬度的地方进行距离排序然后返回给用户
例如一些外卖app打开会返回附近的商店,这个是怎么做到的呢?

思路一:

根据用户当前的位置,用计算经纬度距离的算法逐一计算比对距离,然后进行排序。这里可以参考下面这个算法:

<?php/** * 查找两个经纬度之间的距离 * * @param ?$latitude1 ??float ?起始纬度 * @param ?$longitude1 ?float ?起始经度 * @param ?$latitude2 ??float ?目标纬度 * @param ?$longitude2 ?float ?目标经度 * @return array(miles=>英里,feet=>英尺,yards=>码,kilometers=>公里,meters=>米) * @example * * ????????$point1 = array(‘lat‘ => 40.770623, ‘long‘ => -73.964367); * ????????$point2 = array(‘lat‘ => 40.758224, ‘long‘ => -73.917404); * ????????$distance = getDistanceBetweenPointsNew($point1[‘lat‘], $point1[‘long‘], $point2[‘lat‘], $point2[‘long‘]); * ????????foreach ($distance as $unit => $value) { * ????????????echo $unit.‘: ‘.number_format($value,4); * ????????} * * ????????The example returns the following: * * ????????miles: 2.6025 ??????//英里 * ????????feet: 13,741.4350 ??//英尺 * ????????yards: 4,580.4783 ??//码 * ????????kilometers: 4.1884 ?//公里 * ????????meters: 4,188.3894 ?//米 * */function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) { ???$theta = $longitude1 - $longitude2; ???$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); ???$miles = acos($miles); ???$miles = rad2deg($miles); ???$miles = $miles * 60 * 1.1515; ???$feet = $miles * 5280; ???$yards = $feet / 3; ???$kilometers = $miles * 1.609344; ???$meters = $kilometers * 1000; ???return compact(‘miles‘, ‘feet‘, ‘yards‘, ‘kilometers‘, ‘meters‘);}?>

这个思路是要每次都获取全部数据,然后进行不断的循环计算,对于大数据量来说简直是噩梦。

思路二:

利用二维的经纬度转换成一维的数据,然后直接sql查询,无须一一比对。
例如经纬度 110.993736,21.495705 => w7yfm9pjt9b4
这就是geohash算法,这里简单说一下,geohash是一种地理位置编码,通过数学的方法进行一定的转换,使其与经纬度对应,变成一串可比对的字符串。
这里不做深入的了解,大概知道一下就好,转换出来的编码有一定的规律,例如同一个省份的前几位字符是一样的,字符数相似越多,证明距离越近。类似于公民身份证一样。
有兴趣的可以自行搜索了解一下。
下面直接给出转换的php代码

<?php/** * Encode and decode geohashes * */class Geohash { ???private $coding = "0123456789bcdefghjkmnpqrstuvwxyz"; ???private $codingMap = array(); ???public function Geohash() { ???????//build map from encoding char to 0 padded bitfield ???????for ($i = 0; $i < 32; $i++) { ???????????$this->codingMap[substr($this->coding, $i, 1)] = str_pad(decbin($i), 5, "0", STR_PAD_LEFT); ???????} ???} ???/** ????* Decode a geohash and return an array with decimal lat,long in it ????*/ ???public function decode($hash) { ???????//decode hash into binary string ???????$binary = ""; ???????$hl = strlen($hash); ???????for ($i = 0; $i < $hl; $i++) { ???????????$binary .= $this->codingMap[substr($hash, $i, 1)]; ???????} ???????//split the binary into lat and log binary strings ???????$bl = strlen($binary); ???????$blat = ""; ???????$blong = ""; ???????for ($i = 0; $i < $bl; $i++) { ???????????if ($i % 2) { ???????????????$blat = $blat . substr($binary, $i, 1); ???????????} else { ???????????????$blong = $blong . substr($binary, $i, 1); ???????????} ???????} ???????//now concert to decimal ???????$lat = $this->binDecode($blat, -90, 90); ???????$long = $this->binDecode($blong, -180, 180); ???????//figure out how precise the bit count makes this calculation ???????$latErr = $this->calcError(strlen($blat), -90, 90); ???????$longErr = $this->calcError(strlen($blong), -180, 180); ???????//how many decimal places should we use? There‘s a little art to ???????//this to ensure I get the same roundings as geohash.org ???????$latPlaces = max(1, -round(log10($latErr))) - 1; ???????$longPlaces = max(1, -round(log10($longErr))) - 1; ???????//round it ???????$lat = round($lat, $latPlaces); ???????$long = round($long, $longPlaces); ???????return array($lat, $long); ???} ???/** ????* Encode a hash from given lat and long ????*/ ???public function encode($lat, $long) { ???????//how many bits does latitude need? ???????$plat = $this->precision($lat); ???????$latbits = 1; ???????$err = 45; ???????while ($err > $plat) { ???????????$latbits++; ???????????$err /= 2; ???????} ???????//how many bits does longitude need? ???????$plong = $this->precision($long); ???????$longbits = 1; ???????$err = 90; ???????while ($err > $plong) { ???????????$longbits++; ???????????$err /= 2; ???????} ???????//bit counts need to be equal ???????$bits = max($latbits, $longbits); ???????//as the hash create bits in groups of 5, lets not ???????//waste any bits - lets bulk it up to a multiple of 5 ???????//and favour the longitude for any odd bits ???????$longbits = $bits; ???????$latbits = $bits; ???????$addlong = 1; ???????while (($longbits + $latbits) % 5 != 0) { ???????????$longbits += $addlong; ???????????$latbits += !$addlong; ???????????$addlong = !$addlong; ???????} ???????//encode each as binary string ???????$blat = $this->binEncode($lat, -90, 90, $latbits); ???????$blong = $this->binEncode($long, -180, 180, $longbits); ???????//merge lat and long together ???????$binary = ""; ???????$uselong = 1; ???????while (strlen($blat) + strlen($blong)) { ???????????if ($uselong) { ???????????????$binary = $binary . substr($blong, 0, 1); ???????????????$blong = substr($blong, 1); ???????????} else { ???????????????$binary = $binary . substr($blat, 0, 1); ???????????????$blat = substr($blat, 1); ???????????} ???????????$uselong = !$uselong; ???????} ???????//convert binary string to hash ???????$hash = ""; ???????for ($i = 0; $i < strlen($binary); $i += 5) { ???????????$n = bindec(substr($binary, $i, 5)); ???????????$hash = $hash . $this->coding[$n]; ???????} ???????return $hash; ???} ???/** ????* What‘s the maximum error for $bits bits covering a range $min to $max ????*/ ???private function calcError($bits, $min, $max) { ???????$err = ($max - $min) / 2; ???????while ($bits--) { ???????????$err /= 2; ???????} ???????return $err; ???} ???/* ????* returns precision of number ????* precision of 42 is 0.5 ????* precision of 42.4 is 0.05 ????* precision of 42.41 is 0.005 etc ????*/ ???private function precision($number) { ???????$precision = 0; ???????$pt = strpos($number, ‘.‘); ???????if ($pt !== false) { ???????????$precision = -(strlen($number) - $pt - 1); ???????} ???????return pow(10, $precision) / 2; ???} ???/** ????* create binary encoding of number as detailed in http://en.wikipedia.org/wiki/Geohash#Example ????* removing the tail recursion is left an exercise for the reader ????*/ ???private function binEncode($number, $min, $max, $bitcount) { ???????if ($bitcount == 0) { ???????????return ""; ???????} ???????#echo "$bitcount: $min $max<br>"; ???????//this is our mid point - we will produce a bit to say ???????//whether $number is above or below this mid point ???????$mid = ($min + $max) / 2; ???????if ($number > $mid) { ???????????return "1" . $this->binEncode($number, $mid, $max, $bitcount - 1); ???????} else { ???????????return "0" . $this->binEncode($number, $min, $mid, $bitcount - 1); ???????} ???} ???/** ????* decodes binary encoding of number as detailed in http://en.wikipedia.org/wiki/Geohash#Example ????* removing the tail recursion is left an exercise for the reader ????*/ ???private function binDecode($binary, $min, $max) { ???????$mid = ($min + $max) / 2; ???????if (strlen($binary) == 0) { ???????????return $mid; ???????} ???????$bit = substr($binary, 0, 1); ???????$binary = substr($binary, 1); ???????if ($bit == 1) { ???????????return $this->binDecode($binary, $mid, $max); ???????} else { ???????????return $this->binDecode($binary, $min, $mid); ???????} ???}}?>

把每一个经纬度都转换成geohash编码并储存起来,比对的时候直接sql

$sql = ‘select * from xxx where geohash like "‘.$like_geohash.‘%"‘;

这里like_geohash位数越多说明越精确。
下面是geohash经度距离换算关系,比如geohash如果有7位数,说明范围在76米左右,八位数则是19米,可以根据这个进行查询。

geohash长度Lat位数Lng位数Lat误差Lng误差km误差
123±23±23±2500
255± 2.8±5.6±630
378± 0.70± 0.7±78
41010± 0.087± 0.18±20
51213± 0.022± 0.022±2.4
61515± 0.0027± 0.0055±0.61
71718±0.00068±0.00068±0.076
82020±0.000086±0.000172±0.01911
92223±0.000021±0.000021±0.00478
102525±0.00000268±0.00000536±0.0005971
112728±0.00000067±0.00000067±0.0001492
123030±0.00000008±0.00000017±0.0000186

这个思路明显优于第一个思路,且查询起来速度非常快,也不用管有多大的数据,直接在数据库里面进行like查询就好,不过要做好索引才行,缺点也是比较明显
无法控制想要的精确访问,对于返回的数据无法进行距离的先后排序,不过已经能满足一定的需求,后期再结合思路一也可以做到距离的先后。

思路三:

前面两种方法都是通过很生硬的数学方法进行比对,所计算的也都是直线距离,但是现实并不是数学那样理想。
现实中两个很靠近的经纬度中间也有可能隔着一条跨不过去的河导致要绕很远的路,这时就要考虑实际情况。
很庆幸有些地图厂商已经帮我们考虑到了,所以还可以借助第三方api。
这里简单说一下高德地图的[云图服务API](http://lbs.amap.com/api/yuntu)
1、注册高德地图账户,并申请云图key。
2、创建云地图,也就是把你现在的数据放到高德地图上 [云图存储API](http://lbs.amap.com/api/yuntu/guide/data/storage),这里可以手动创建也能调用相关的api创建。
可以把数据导出excel然后批量上传,当然后期如果要新增尽量还是用它提供的接口进行增量添加。创建完成大概会生成这样一张表,有tableid,这个后面查询接口需要使用,字段可以自定义,方便业务逻辑。

3、使用高德api进行查询你的云地图 [数据检索](http://lbs.amap.com/api/yuntu/guide/data/search) ,这里使用周边检索,可以根据你当前的位置进行检索。
过程其实也不复杂,就是把数据放到高德,高德帮你完成了距离的排序,当然它提供的是比较实际的距离。具体实现需要研究一下高德提供的接口。
这个思路可以解决精准度问题,但开发成本大,还要跑一遍第三方去获取数据,可能会牺牲一定效率,具体取舍,仁者见仁吧。

php LBS(附近地理位置)功能实现的一些思路

原文地址:https://www.cnblogs.com/chriiess/p/9078620.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved