Redis GEODIST – How to get distance between two members of geospatial value

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get the distance between two members of geospatial value stored at a key. For this we will use a Redis GEODIST command.

GEODIST Command

This command is used to return the distance between two members of geospatial value ( Sorted Set ) stored at a key in specified unit. If one or both specified members does not exist in the geospatial value, then null is returned.

The specified unit must be of the following type :- 

    1. m for meter ( default ).
    2. km for kilometer.
    3. mi for miles.
    4. ft for feet.

Nil is returned when key does not exist and error is returned when key exist but value stored at the key is not of sorted set datatype, populated using GEOADD command. The syntax of redis GEODIST command is as follows :-

Syntax :-

redis host:post> GEODIST <keyname> <member-1> <member-2> [unit]

Output :- 

- (string) reply, representing the distance in specified unit.
- Nil, if key does not exist.
- Error, if key exist and value stored at the key is not a sorted set populated using GEOADD command.

Example :-

Redis GEODIST

References :-

  1. GEODIST Command Docs

That’s all for how to get the distance between two members of geospatial value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.

<- Geo Commands