方案一:
现在很多手机软件都用附近搜索功能,但具体是怎么实现的呢》
在网上查了很多资料,mysql空间数据库、矩形算法、geohash我都用过了,当数据上了百万之后mysql空间数据库方法是最强最精确的(查询前100条数据只需5秒左右)。
接下来推出一个原创计算方法,查询速度是mysql空间数据库算法的2倍
$lng是你的经度,$lat是你的纬度
SELECT lng,lat,
(POWER(MOD(ABS(lng - $lng),360),2) + POWER(ABS(lat - $lat),2)) AS distance
FROM `user_location`
ORDER BY distance LIMIT 100
经测试,在100万数据中取前100条数据只需2.5秒左右。
####################################
另外的几种算法还是在这里展示一下:
一、距形算法
define(EARTH_RADIUS, 6371);//地球半径,平均半径为6371km
/**
*计算某个经纬度的周围某段距离的正方形的四个点
*
*@param lng float 经度
*@param lat float 纬度
*@param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米
*@return array 正方形的四个点的经纬度坐标
*/
function returnSquarePoint($lng, $lat,$distance = 0.5){
$dlng = 2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));
$dlng = rad2deg($dlng);
$dlat = $distance/EARTH_RADIUS;
$dlat = rad2deg($dlat);
return array(
'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
);
}
//使用此函数计算得到结果后,带入sql查询。
$squares = returnSquarePoint($lng, $lat);
$info_sql = "select id,locateinfo,lat,lng from `lbs_info` where lat<>0 and lat>{$squares['right-bottom']['lat']} and lat<{$squares['left-top']['lat']} and lng>{$squares['left-top']['lng']} and lng<{$squares['right-bottom']['lng']} ";
java代码如下:
/**
* 默认地球半径
*/
private static double EARTH_RADIUS = 6371;
/**
* 计算经纬度点对应正方形4个点的坐标
*
* @param longitude
* @param latitude
* @param distance
* @return
*/
public static Map<String, double[]> returnLLSquarePoint(double longitude,
double latitude, double distance) {
Map<String, double[]> squareMap = new HashMap<String, double[]>();
// 计算经度弧度,从弧度转换为角度
double dLongitude = 2 * (Math.asin(Math.sin(distance
/ (2 * EARTH_RADIUS))
/ Math.cos(Math.toRadians(latitude))));
dLongitude = Math.toDegrees(dLongitude);
// 计算纬度角度
double dLatitude = distance / EARTH_RADIUS;
dLatitude = Math.toDegrees(dLatitude);
// 正方形
double[] leftTopPoint = { latitude + dLatitude, longitude - dLongitude };
double[] rightTopPoint = { latitude + dLatitude, longitude + dLongitude };
double[] leftBottomPoint = { latitude - dLatitude,
longitude - dLongitude };
double[] rightBottomPoint = { latitude - dLatitude,
longitude + dLongitude };
squareMap.put("leftTopPoint", leftTopPoint);
squareMap.put("rightTopPoint", rightTopPoint);
squareMap.put("leftBottomPoint", leftBottomPoint);
squareMap.put("rightBottomPoint", rightBottomPoint);
return squareMap;
}
二、 空间数据库算法
以下location字段是跟据经纬度来生成的空间数据,如:
location字段的type设为point
"update feed set location=GEOMFROMTEXT('point({$lat} {$lng})') where id='{$id}'"
mysql空间数据查询
SET @center = GEOMFROMTEXT('POINT(35.801559 -10.501577)');
SET @radius = 4000;
SET @bbox = CONCAT('POLYGON((',
X(@center) - @radius, ' ', Y(@center) - @radius, ',',
X(@center) + @radius, ' ', Y(@center) - @radius, ',',
X(@center) + @radius, ' ', Y(@center) + @radius, ',',
X(@center) - @radius, ' ', Y(@center) + @radius, ',',
X(@center) - @radius, ' ', Y(@center) - @radius, '))'
);
SELECT id,lng,lat,
SQRT(POW( ABS( X(location) - X(@center)), 2) + POW( ABS(Y(location) - Y(@center)), 2 )) AS distance
FROM `user_location` WHERE 1=1
AND INTERSECTS( location, GEOMFROMTEXT(@bbox) )
AND SQRT(POW( ABS( X(location) - X(@center)), 2) + POW( ABS(Y(location) - Y(@center)), 2 )) < @radius
ORDER BY distance LIMIT 20
geo算法
参考文档:
http://blog.csdn.net/wangxiafghj/article/details/9014363geohash 算法原理及实现方式
http://blog.charlee.li/geohash-intro/ geohash:用字符串实现附近地点搜索
http://blog.sina.com.cn/s/blog_7c05385f0101eofb.html 查找附近点--Geohash方案讨论
http://www.wubiao.info/372 查找附近的xxx 球面距离以及Geohash方案探讨
http://en.wikipedia.org/wiki/Haversine_formula Haversine formula球面距离公式
http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe 球面距离公式代码实现
http://developer.baidu.com/map/jsdemo.htm#a6_1 球面距离公式验证
http://www.wubiao.info/470 Mysql or Mongodb LBS快速实现方案
geohash有以下几个特点:
首先,geohash用一个字符串表示经度和纬度两个坐标。某些情况下无法在两列上同时应用索引 (例如MySQL 4之前的版本,Google App Engine的数据层等),利用geohash,只需在一列上应用索引即可。
其次,geohash表示的并不是一个点,而是一个矩形区域。比如编码wx4g0ec19,它表示的是一个矩形区域。 使用者可以发布地址编码,既能表明自己位于北海公园附近,又不至于暴露自己的精确坐标,有助于隐私保护。
第三,编码的前缀可以表示更大的区域。例如wx4g0ec1,它的前缀wx4g0e表示包含编码wx4g0ec1在内的更大范围。 这个特性可以用于附近地点搜索。首先根据用户当前坐标计算geohash(例如wx4g0ec1)然后取其前缀进行查询 (SELECT * FROM place WHERE geohash LIKE 'wx4g0e%'),即可查询附近的所有地点。
查找附近网点geohash算法及实现 (Java版本),geohashjava
Geohash比直接用经纬度的高效很多。
Geohash算法实现(Java版本)
package com.DistTest;
import java.util.BitSet;
import java.util.HashMap;
public class Geohash {
private static int numbits = 6 * 5;