Redis GEOPOS – How to get longitude, latitude of multiple members of geospatial value

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get longitude, latitude of one or more members of geospatial value stored at a key. For this we will use a Redis GEOPOS command.

GEOPOS Command

This command is used to return the longitude, latitude of one or more specified members of geospatial value ( Sorted Set ) stored at a key. When coordinates ( longitude, latitude ) are inserted into geospatial value by using GEOADD command, they are converted into a 52 bit geohash. So when coordinates are returned, they may not be exactly the same one used during insertion operation but can contain small errors. It returns an array where each element is an array of two elements representing longitude and latitude of each specified member member.

Empty array 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 which is populated using GEOADD command.

The syntax of redis GEOPOS command is as follows :-

Syntax :-

redis host:post> GEOPOS <member> [member]

Output :- 

- (string) reply, representing the longitude and latitude of specified members.
- Empty array, 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 GEOPOS

References :-

  1. GEOPOS Command Docs

That’s all for how to get the longitude, latitude of one or more 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