首先下载Redis 3.2
wget http://download.redis.io/releases/redis-3.2.3.tar.gz
tar xzf redis-3.2.3.tar.gz
cd redis-3.2.3
make
src/redis-server redis.conf (后面是配置文件)
启动操作工具:
Reis-cli
src/redis-cli -h 127.0.0.1 -p 6399 --raw(能保证中文输出而不是"\xd6\xd0")
添加位置:
GEOADD beijing 113.2099647 23.593675 五道口
GEOADD beijing 113.2099643 23.593674 上地
获取位置:
127.0.0.1:6399> GEOPOS beijing 上地
113.20996195077896118
23.59367501967128788
支持多给位置的查询:
获取指定范围内地点:
GEORADIUS beijing 113.2099647 23.593675 900 m
获取两点之间的距离
GEODIST beijing 上地 五道口
(注:所有返回的单位默认都是M,此处坐标是随便写的)
Python 代码调用:
添加以及查询
def add(request):
if not request.user.is_authenticated:
return HttpResponse("error")
title = request.GET['title']
lat = request.GET['lat']
lng = request.GET['lng']
r = redis.Redis(host='127.0.0.1', port=6399, db=0)
values = (lng,lat,title)
save = r.geoadd("beijing",*values)
return HttpResponseRedirect("/lbs/index")
def get_geo(request):
r = redis.Redis(host='127.0.0.1', port=6399, db=0)
lat = request.GET['lat']
lng = request.GET['lng']
back = r.georadius('beijing',lng, lat, 900,sort="ASC",count=10)
reg_list = r.geopos("beijing",*back)
send_list = []
for s in range(len(back)):
send_list.append({"title":back[s],"lat":reg_list[s][1],"lng":reg_list[s][0]})
send_msg = json.dumps(send_list)
return HttpResponse(send_msg)