In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to use redis to achieve the function of people nearby, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Preface
Redis has provided GEO (geograph) function since version 3.2, which supports location-related operations to achieve functions that depend on location information, such as people in the neighborhood.
Tool Baidu longitude and latitude picker
Pick coordinate system
1. Test data
120.70012 28.00135 Wenzhou
120.207686 30.253359 Hangzhou
121.482537 31.238034 Shanghai
118.793117 32.068407 Nanjing
II. Basic orders
1. Geoadd
In order to perform location-related operations, we first need to record the specific geographic location, which can be completed by executing the geoadd command in the following format
GEOADD collection name longitude and latitude name [longitude latitude name...] / / add collection geoadd citys 120.70012 28.00135 wenzhou 120.207686 30.253359 hanghzou
View added collections
2.geopos
This command gets the information coordinates of the location based on the location name entered. The syntax is as follows.
GEOPOS collection name location [name...] geopos address wuyue
View coordinate information
3.geodist
This command is used to calculate the distance between two locations. The syntax is as follows
Geodist collection name location 1 location 2 [unit] / / calculate the distance between Hangzhou and Nanjing geodist citys hanghzredis:0 > ou nanjing km
Optional parameter: unit is used to specify the unit in which the distance is calculated, and its value can be one of the following units
M: represents meters
Km: indicates kilometer
Mi: stands for miles
Ft: stands for feet.
4.georadius
Georadius uses the latitude and longitude given by the user as the center point when calculating the range
Georadius collection name precision latitude radius m | km | ft | mi | [WITHCOORD] [WITHDIST] [ASC | DESC] [COUNT count] / / query the cities within my 100km georadius citys 120.754274 27.983296 100km
Radius: distanc
WITHCOORD: returns coordinates
May be empty due to version
WITHDIST: return distance at the same time
ASC | DESC: sort
Count: how long to take
5. Georadiusbymember
Georadiusbymember uses a location stored in the location collection as the central point
Georadiusbymember address collection location name georadiusbymember address wuyue 5 km within five kilometers of unit / / query distance from Wuyue
III. JavaApi
Entity class
Package com.jiale.web.controller.pojo; import lombok.Data; import java.io.Serializable; / * * @ Author: Hello World * @ Date: 16:12 on 2021-9-16 * @ Description: * / @ Datapublic class AddressInfo implements Serializable {/ * Network name * / private String title; / * * Network address * / private String address; / * * Network contact information * / private String phone / * * dot coordinates-precision * / private double x; / * * dot coordinates-latitude * / private double y;} package com.jiale; import com.jiale.common.config.JialeGlobal;import com.jiale.web.controller.pojo.AddressInfo;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.geo.*;import org.springframework.data.redis.connection.RedisGeoCommands Import org.springframework.data.redis.core.RedisTemplate;import org.springframework.web.bind.annotation.RequestMapping; import java.math.BigDecimal;import java.util.List; / * * @ Author: Hello World * @ Date: 17:47 on 2021-9-15 * @ Description: * / @ SpringBootTestpublic class JialeTests {@ Autowired RedisTemplate redisTemplate; @ Testpublic void test1 () {System.out.println (JialeGlobal.getUploadPath ());} / * one. Add location information to redis * 1. Online store location information-geo * 2. Network details * / @ Test public void geoAdd () {/ / 1. The location information of the site is stored as 120.653208 Point point 28.032606 Point point = new Point (120.653208Magi 28.032606); redisTemplate.boundGeoOps ("outlets") .add (point, "Jiangxinyu"); / / 2. Site details AddressInfo addressInfo = new AddressInfo (); addressInfo.setTitle ("Jiangxinyu"); addressInfo.setAddress ("Jiangxinyu Scenic spot, Lucheng District, Wenzhou City, Zhejiang Province"); addressInfo.setPhone ("(0577) 88201281"); addressInfo.setX (120.653208); addressInfo.setY (28.032606); redisTemplate.boundHashOps ("outletsInfo"). Put (Jiangxinyu) } / * * two. Get location coordinate information * / @ Test public void geoPos () {/ / input location name query location information List position = redisTemplate.boundGeoOps ("outlets"). Position ("Nantang five groups"); for (Point point: position) {System.out.println (point);}} / * III. Calculate two location information * / @ Test public void geoDist () {/ * * Distance distance = redisTemplate.boundGeoOps ("outlets") .distance ("Jiangxinyu", "Wenzhou Paradise"); / / distance double value = distance.getValue (); / / Unit String unit = distance.getUnit (); System.out.println ("distance between two points:" + value+unit) * / / displayed in km / * * Distance distance = redisTemplate.boundGeoOps ("outlets"). Distance ("Jiangxinyu", "Wenzhou Paradise", Metrics.KILOMETERS); / / distance double value = distance.getValue (); / / Unit String unit = distance.getUnit (); System.out.println ("two points apart:" + value+unit) * / / keep two decimal places Distance distance = redisTemplate.boundGeoOps ("outlets"). Distance ("Jiangxinyu", "Wenzhou Paradise", Metrics.KILOMETERS); / / distance double value = distance.getValue (); / / Unit String unit = distance.getUnit (); System.out.println ("distance between two points:" + new BigDecimal (value). SetScale (2Magnum BigDecimal.ROUNDgroundHALFupUp) + unit) } / * * four. Find the location of the specified range according to the given longitude and latitude * / @ Test public void geoRadius () {/ / specified location Point point = new Point (120.754274, 27.983296); / / Construction condition-10km Distance distance = new Distance (10, Metrics.KILOMETERS); Circle circle = new Circle (point,distance); / / RedisGeoCommands RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs () / / contains location information args.includeDistance (); / / stores the location information of the queried URL GeoResults outlets = redisTemplate.boundGeoOps ("outlets") .radius (circle, args); for (GeoResult outlet: outlets) {/ / get distance information Distance outletDistance = outlet.getDistance () / / distance double value = outletDistance.getValue (); / / Unit String unit = outletDistance.getUnit (); System.out.println ("current coordinate distance:" + outlet.getContent (). GetName () + value+unit);}} / * five. Find the specified scope location * / @ Test public void geoRadiusByMember () {/ / build condition Distance distance = new Distance (10, Metrics.KILOMETERS) according to the specified element (which must exist in the collection); / / build condition-contains location information RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs (); args.includeDistance () / / conditions constructed by key other conditions GeoResults outlets = redisTemplate.boundGeoOps ("outlets"). Radius ("Jiangxin Island", distance,args); for (GeoResult outlet: outlets) {/ / get distance information Distance outletDistance = outlet.getDistance (); / / distance double value = outletDistance.getValue () / / Unit String unit = outletDistance.getUnit (); System.out.println ("Jiangxinyu distance:" + outlet.getContent () .getName () + value+unit) } this is the answer to the question on how to use redis to implement the human function nearby. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.