In the previous article “SpringBoot Tutorial (5)Java based on MySQL implementation of nearby people,” we introduced Java based on MySQL implementation to find nearby people function. Today we’re going to look at another solution to find people nearby, a Redis-based solution.

VGEO location

Version available: >= 3.2.0

Geography provides approximately six commands, which are:

  • GEOADD
  • GEODIST
  • GEOHASH
  • GEOPOS
  • GEORADIUS
  • GEORADIUSBYMEMBER

vGEOADD

1.1 GEOADD concept

Adds the given space element (latitude, longitude, name) to the specified key. The data is stored in the keys as an ordered collection, allowing commands like GEORADIUS and GEORADIUSBYMEMBER to retrieve the elements later through location queries.

The GEOADD command takes arguments in a standard X, Y format, so the user must enter longitude first, then latitude. The coordinates GEOADD can record are limited: regions very close to the poles cannot be indexed. Precise coordinate limits are defined by coordinate systems such as EPSG:900913 / EPSG:3785 / OSGEO:41001 as follows:

The effective longitude is between -180 degrees and 180 degrees.

The effective latitude ranges from -85.05112878 degrees to 85.05112878 degrees.

When the user tries to enter an out-of-range longitude or latitude, the GEOADD command returns an error.

1.2 GEOADD command

Time complexity: The complexity of each element added is O(log(N)), where N is the number of positional elements contained in the key.

GEOADD key longitude latitude member [longitude latitude member…]

Command description: Adds the specified geospatial location (latitude, longitude, name) to the specified key.

Return value: The number of elements added to the sorted set, but not the elements whose score has been updated.

1.3 GEOADD sample

vGEODIST

2.1 GEODIST concept

Returns the distance between two given positions.

If one of the two positions does not exist, the command returns a null value.

The parameter unit specifying the unit must be one of the following:

  • m Represents the unit in meters.
  • km The unit is kilometers.
  • mi Represents miles.
  • ft The unit is feet.

If the user does not explicitly specify a unit parameter, GEODIST defaults to using meters.

The GEODIST command calculates distances by assuming that the Earth is perfectly spherical, an assumption that results in an error of up to 0.5% in extreme cases.

2.2 GEODIST command

Complexity: ORDER log(N)

Command demo: GEODIST key member1 member2 [unit]

2.3 GEODIST sample

vGEOPOS

3.1 GEOPOS concept

Returns the position (latitude and longitude) of all elements at a given position from within the key.

Because GEOPOS takes a variable number of location elements as input, the command returns an array reply even if the user has given only one location element.

The GEOPOS command returns an array in which each entry consists of two elements: the first is the longitude of the element at a given location, and the second is the latitude of the element at a given location. When the given location element does not exist, the corresponding array entry is null.

3.2 GEOPOS command

Time complexity: The complexity of obtaining each positional element is O(log(N)), where N is the number of positional elements contained in the key.

Command demo: GEOPOS key member [member…]

3.2 GEOPOS sample

vGEOHASH

4.1 GEOHASH concept

Returns a Geohash representation of one or more positional elements.

Return value: an array, each of which is a GeoHash. The position of the geohash returned by the command corresponds to the position of the user-given location element.

4.2 GEOHASH command

Time complexity: The complexity of finding each positional element is O(log(N)), where N is the number of positional elements contained in a given key.

Demo: GEOHASH key member [member…

Command description: Returns a Geohash representation of one or more positional elements. It is common to use a different technique using elements that represent positions, using Geohash position 52-point integer encoding. The encoding is also different from the standard due to the different initial minimum and maximum coordinates used in the encoding and decoding process. This command returns a standard Geohash

4.3 GEOHASH sample

vGEORADIUS

5.1 GEORADIUS concept

Returns all of the location elements contained by the key that are within a given maximum distance from the center, centered at a given latitude and longitude.

A range can use one of the following units:

  • m Represents the unit in meters.
  • km The unit is kilometers.
  • mi Represents miles.
  • ft The unit is feet.

The command returns additional information when given the following options:

WITHDIST: The distance between the location element and the center is also returned. The units of distance are the same as the units of range given by the user.

WITHCOORD: Returns the longitude and dimension of the positional element as well.

WITHHASH: Returns the original Geohashed ordered set score of the positional element as a 52-bit signed integer. This option is mainly used for low-level applications or debugging, and is not useful in practice.

The command returns unsorted positional elements by default. The user can specify the order of the elements of the returned position with the following two parameters:

ASC: Returns the position element from near to far based on the position of the center.

DESC: Returns the location element from far to near based on the location of the center.

By default, the GEORADIUS command returns all matching positional elements. Although the user can use the COUNT option to retrieve the first N matched elements, because the command may need to process all matched elements internally, even using the COUNT option to retrieve only a few elements in a very large area of a search can be very slow. On the other hand, using the COUNT option to reduce the number of elements that need to be returned is still very useful for reducing bandwidth.

The GEORADIUS command returns an array, specifically:

  • If no WITH option is given, the command simply returns a linear list like [“New York”,”Milan”,”Paris”].
  • With WITHCOORD, WITHDIST, WITHHASH, etc., the command returns a two-layer nested array, with each subarray of the inner layer representing an element.

When returning a nested array, the first element of the subarray is always the name of the positional element. Additional information is returned as subsequent elements of the subarray in the following order:

  • The distance between the center and position elements returned in floating-point format, in the same units as when the user specified a range.
  • Geohash integer.
  • A coordinate consisting of two elements, longitude and latitude.

5.2 GEORADIUS command

Time complexity: O(N+log(M)), where N is the number of positional elements within the specified radius and M is the number of positional elements returned.

Demo of command: GEORADIUS key longitude latitude | km radius m | ft | mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT COUNT]

5.3 GEORADIUS sample

vGEORADIUSBYMEMBER

6.1 GEORADIUSBYMEMBER concept

This command, like the GEORADIUS command, can find the elements in the specified range, but the center point of GEORADIUSBYMEMBER is determined by the given location element, instead of using the longitude and latitude of input to determine the center point, as GEORADIUS did.

Time complexity: O(log(N)+M), where N is the number of elements in the specified range and M is the number of elements returned.

6.2 GEORADIUSBYMEMBER command

Command: GEORADIUSBYMEMBER key member | km radius m | ft | mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT COUNT]

6.3 GEORADIUSBYMEMBER sample

Used in vSpingboot

        RedisTemplate redisTemplate;
        redisTemplate.opsForGeo().geoAdd()
        redisTemplate.opsForGeo().geoDist()
        redisTemplate.opsForGeo().geoHash()
        redisTemplate.opsForGeo().geoPos()
        redisTemplate.opsForGeo().geoRadius()
        redisTemplate.opsForGeo().geoRadiusByMember()
Copy the code

V Source code address

Github.com/toutouge/ja…

About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \