现在很多手机软件都用附近搜索功能,但具体是怎么实现的呢》
在网上查了很多资料,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秒左右。
###########################################
另外的几种算法还是在这里展示一下:
ps:赤道半径 6378.137km
平均地球半径 6371.004km
一、距形算法
1 define(EARTH_RADIUS, 6371);//地球半径,平均半径为6371km 2 /** 3 *计算某个经纬度的周围某段距离的正方形的四个点 4 * 5 *@param lng float 经度 6 *@param lat float 纬度 7 *@param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米 8 *@return array 正方形的四个点的经纬度坐标 9 */ 10 function returnSquarePoint($lng, $lat,$distance = 0.5){ 11 12 $dlng = 2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat))); 13 $dlng = rad2deg($dlng); 14 15 $dlat = $distance/EARTH_RADIUS; 16 $dlat = rad2deg($dlat); 17 18 return array( 19 'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng), 20 'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng), 21 'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng), 22 'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng) 23 ); 24 } 25 //使用此函数计算得到结果后,带入sql查询。 26 $squares = returnSquarePoint($lng, $lat); 27 $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代码如下:
1 /** 2 * 默认地球半径 3 */ 4 private static double EARTH_RADIUS = 6371; 5 6 /** 7 * 计算经纬度点对应正方形4个点的坐标 8 * 9 * @param longitude 10 * @param latitude 11 * @param distance 12 * @return 13 */ 14 public static Map<String, double[]> returnLLSquarePoint(double longitude, 15 double latitude, double distance) { 16 Map<String, double[]> squareMap = new HashMap<String, double[]>(); 17 // 计算经度弧度,从弧度转换为角度 18 double dLongitude = 2 * (Math.asin(Math.sin(distance 19 / (2 * EARTH_RADIUS)) 20 / Math.cos(Math.toRadians(latitude)))); 21 dLongitude = Math.toDegrees(dLongitude); 22 // 计算纬度角度 23 double dLatitude = distance / EARTH_RADIUS; 24 dLatitude = Math.toDegrees(dLatitude); 25 // 正方形 26 double[] leftTopPoint = { latitude + dLatitude, longitude - dLongitude }; 27 double[] rightTopPoint = { latitude + dLatitude, longitude + dLongitude }; 28 double[] leftBottomPoint = { latitude - dLatitude, 29 longitude - dLongitude }; 30 double[] rightBottomPoint = { latitude - dLatitude, 31 longitude + dLongitude }; 32 squareMap.put("leftTopPoint", leftTopPoint); 33 squareMap.put("rightTopPoint", rightTopPoint); 34 squareMap.put("leftBottomPoint", leftBottomPoint); 35 squareMap.put("rightBottomPoint", rightBottomPoint); 36 return squareMap; 37 }
二、 空间数据库算法
以下location字段是跟据经纬度来生成的空间数据,如:
location字段的type设为point
"update feed set location=GEOMFROMTEXT('point({$lat} {$lng})') where id='{$id}'"
mysql空间数据查询
1 SET @center = GEOMFROMTEXT('POINT(35.801559 -10.501577)'); 2 SET @radius = 4000; 3 SET @bbox = CONCAT('POLYGON((', 4 X(@center) - @radius, ' ', Y(@center) - @radius, ',', 5 X(@center) + @radius, ' ', Y(@center) - @radius, ',', 6 X(@center) + @radius, ' ', Y(@center) + @radius, ',', 7 X(@center) - @radius, ' ', Y(@center) + @radius, ',', 8 X(@center) - @radius, ' ', Y(@center) - @radius, '))' 9 ); 10 SELECT id,lng,lat, 11 SQRT(POW( ABS( X(location) - X(@center)), 2) + POW( ABS(Y(location) - Y(@center)), 2 )) AS distance 12 FROM `user_location` WHERE 1=1 13 AND INTERSECTS( location, GEOMFROMTEXT(@bbox) ) 14 AND SQRT(POW( ABS( X(location) - X(@center)), 2) + POW( ABS(Y(location) - Y(@center)), 2 )) < @radius 15 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版本)
1 package com.DistTest; 2 import java.util.BitSet; 3 import java.util.HashMap; 4 5 public class Geohash { 6 7 private static int numbits = 6 * 5; 8 final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', 9 '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 10 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; 11 12 final static HashMap<Character, Integer> lookup = new HashMap<Character, Integer>(); 13 static { 14 int i = 0; 15 for (char c : digits) 16 lookup.put(c, i++); 17 } 18 19 public double[] decode(String geohash) { 20 StringBuilder buffer = new StringBuilder(); 21 for (char c : geohash.toCharArray()) { 22 23 int i = lookup.get(c) + 32; 24 buffer.append( Integer.toString(i, 2).substring(1) ); 25 } 26 27 BitSet lonset = new BitSet(); 28 BitSet latset = new BitSet(); 29 30 //even bits 31 int j =0; 32 for (int i=0; i< numbits*2;i+=2) { 33 boolean isSet = false; 34 if ( i < buffer.length() ) 35 isSet = buffer.charAt(i) == '1'; 36 lonset.set(j++, isSet); 37 } 38 39 //odd bits 40 j=0; 41 for (int i=1; i< numbits*2;i+=2) { 42 boolean isSet = false; 43 if ( i < buffer.length() ) 44 isSet = buffer.charAt(i) == '1'; 45 latset.set(j++, isSet); 46 } 47 //中国地理坐标:东经73°至东经135°,北纬4°至北纬53° 48 double lon = decode(lonset, 70, 140); 49 double lat = decode(latset, 0, 60); 50 51 return new double[] {lat, lon}; 52 } 53 54 private double decode(BitSet bs, double floor, double ceiling) { 55 double mid = 0; 56 for (int i=0; i<bs.length(); i++) { 57 mid = (floor + ceiling) / 2; 58 if (bs.get(i)) 59 floor = mid; 60 else 61 ceiling = mid; 62 } 63 return mid; 64 } 65 66 67 public String encode(double lat, double lon) { 68 BitSet latbits = getBits(lat, 0, 60); 69 BitSet lonbits = getBits(lon, 70, 140); 70 StringBuilder buffer = new StringBuilder(); 71 for (int i = 0; i < numbits; i++) { 72 buffer.append( (lonbits.get(i))?'1':'0'); 73 buffer.append( (latbits.get(i))?'1':'0'); 74 } 75 return base32(Long.parseLong(buffer.toString(), 2)); 76 } 77 78 private BitSet getBits(double lat, double floor, double ceiling) { 79 BitSet buffer = new BitSet(numbits); 80 for (int i = 0; i < numbits; i++) { 81 double mid = (floor + ceiling) / 2; 82 if (lat >= mid) { 83 buffer.set(i); 84 floor = mid; 85 } else { 86 ceiling = mid; 87 } 88 } 89 return buffer; 90 } 91 92 public static String base32(long i) { 93 char[] buf = new char[65]; 94 int charPos = 64; 95 boolean negative = (i < 0); 96 if (!negative) 97 i = -i; 98 while (i <= -32) { 99 buf[charPos--] = digits[(int) (-(i % 32))]; 100 i /= 32; 101 } 102 buf[charPos] = digits[(int) (-i)]; 103 104 if (negative) 105 buf[--charPos] = '-'; 106 return new String(buf, charPos, (65 - charPos)); 107 } 108 109 }