Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6982459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:17:28+00:00 2026-05-27T18:17:28+00:00

This is my database table (spots). Given a point on map P(longitude, latitude) ,

  • 0

This is my database table (spots).

enter image description here

Given a point on map P(longitude, latitude), what I want to retrieve from the database is the set of points within a given radius where P is the center. I tried a simple query using the simple formula of Longitude and Latitude, unfortunately the result is always null 🙁 . What am I doing wrong here? Any idea?

$sql = "SELECT * FROM $tbl_name 
WHERE ACOS(SIN('".$latitude."') * SIN(spots.latitude) + COS('".$latitude."') * COS(spots.latitude) * COS(spots.longitude - '".$longitude    ."')) * 6371 <= '".$radius."'";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:17:28+00:00Added an answer on May 27, 2026 at 6:17 pm

    Based on Cameron S’s & MrGlass’s ideas, and a bit searching over the Internet, I found one working solution:

    $R = 6371;
    $max_lat = $lat + rad2deg($r/$R);
    $min_lat = $lat - rad2deg($r/$R);
    
    $max_lon = $lon + rad2deg($r/$R/cos(deg2rad($lat)));
    $min_lon = $lon - rad2deg($r/$R/cos(deg2rad($lat)));
    $lat = deg2rad($lat);
    $lon = deg2rad($lon);
    
    $sql = "
    select id, name, description, latitude, longitude, 
           acos(sin($lat)*sin(radians(latitude)) + cos($lat)*cos(radians(latitude))*cos(radians(longitude)-$lon)) * $R as D
    from (
      select id, name, description, latitude, longitude 
      from $tbl_name 
      where latitude > $min_lat and latitude < $max_lat
        and longitude > $min_lon and longitude < $max_lon
      ) as first_cut 
    where acos(sin($lat)*sin(radians(latitude)) + cos($lat)*cos(radians(latitude))*cos(radians(longitude) - $lon)) * $R < $r
    order by D";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm overthinking this. I have colors stored in a database table, and I want
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a database table like this: id version_id field1 field2 1 1 texta
I use this code to update data in database table. Can reuse same code
I have a database table using an enum. This is already working with hibernate
I do this to save new invoince in my Invoices database table: // insert
when i give this sql query in my msaccess database table called warehouse1 it
I have written a function to print database table to an array like this
I always forget how to do things like this. I have a database table
I have created a table on an Oracle 10g database with this structure :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.