Shakhzod Khashimov
Shakhzod Khashimov

Reputation: 47

Trying to query the nearest places in postgres with the help of user latitude, longitude and places latitude,longitude

I'm trying to query the nearest places in postgres with the help of user latitude, longitude and places latitude,longitude with the formula

SELECT * FROM place
WHERE acos(sin(0.90987) * sin(latitude) +
      cos(0.9098) * cos(latitude) * cos(0.3675-longitude)) * 6371
      <= 100;

But I'm getting error saying:

Error: Input is out of range.

who can help?

input is out of range

Upvotes: 0

Views: 175

Answers (1)

JGH
JGH

Reputation: 17906

Assuming the coordinates are in radians, there is a typo near the end (the -longitude must be outside of the last cos)

SELECT * FROM place 
WHERE 
 acos(
   sin(0.90987)
    *sin(latitude)
   +cos(0.9098)
     *cos(latitude)
     *cos(0.3675)
   -longitude)
*6371<=100

That being said, have a look at Postgis, it greatly simplifies spatial queries Select * FROM place where st_dwithin(place.geog, my_target_point, 100)

Upvotes: 1

Related Questions