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

  • SEARCH
  • Home
  • 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 8158245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:32:45+00:00 2026-06-06T17:32:45+00:00

I am having issues trying to return the closest location to the user via

  • 0

I am having issues trying to return the closest location to the user via an SQL statement. To test I am using the exact same coordinates, here is what I have:

SQL:

SELECT  `companies`.`customerName` , 
    (3959 * 
        ACOS(
            COS(
                RADIANS(37.367485) * COS(RADIANS(`locations`.`gps_lat`)) * 
                COS(
                    RADIANS(`locations`.`gps_lng`) - RADIANS(-77.399994) + 
                    SIN(RADIANS(37.367485)) * SIN(RADIANS(`locations`.`gps_lat`))
                )
            )
        )
    )
AS  `distance` 
FROM  `locations` 
JOIN  `companies` ON  `locations`.`co_id` 
HAVING  `distance` > 25
ORDER BY distance
LIMIT 0 , 10

Results:

| customerName  | distance           |
| SOME COMPANY  | 1914.41747964854   |

locations table values:

gps_lat   | gps_lng
37.367485 | -77.399994

I used the example from Google, I checked the formula a couple times and I can’t seem to come up with where I went wrong. Any help is appreciated.


EDIT:
Since there seems to be some confusion on me knowing what I am doing:

The > was substituted to yield a result, 1914 is obviously greater then 25.

The application of this is passing user coordinates from an Android app to our web server. The Latitude and Longitude will be pulled from the $_GET values and cross-referenced from companies in our web servers MYSQL database.

The syntax above is just me checking my SQL statement in Mysql Workbench. Obviously I am looking for a zero value result.

  • 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-06-06T17:32:46+00:00Added an answer on June 6, 2026 at 5:32 pm

    I think I have solved this issue by changing from Googles example to the formula provided by “Adventures of an adopted yooper”. Using his/her version of the Haversine Formula.
    Note: The Google example above is using Haversine as well.

    SQL:

    SELECT  `companies`.`customerName` , 
    (2 * (3959 * ATAN2(
              SQRT(
                POWER(SIN((RADIANS(37.367485 - `locations`.`gps_lat` ) ) / 2 ), 2 ) +
                COS(RADIANS(`locations`.`gps_lat`)) *
                COS(RADIANS(37.367485 )) *
                POWER(SIN((RADIANS(-77.399994 - `locations`.`gps_lng` ) ) / 2 ), 2 )
              ),
              SQRT(1-(
                POWER(SIN((RADIANS(37.367485 - `locations`.`gps_lat` ) ) / 2 ), 2 ) +
                COS(RADIANS(`locations`.`gps_lat`)) *
                COS(RADIANS(37.367485)) *
                POWER(SIN((RADIANS(-77.399994 - `locations`.`gps_lng` ) ) / 2 ), 2 )
              ))
            )
          ))
    AS 'distance'
    FROM  `locations` 
    JOIN  `companies` ON  `locations`.`co_id` 
    HAVING  distance < 25
    ORDER BY distance
    LIMIT 0 , 10
    


    For those curious on my application, Final PHP:

    GET Value: ?lat=37.367485&lng=-77.399994

    $earth_radius_miles = 3959; // Earth radius in miles
    $earth_radius_kilometers = 6371; // Earth radius in kilometers
    $result_radius = 10000; // Maximum distance in either miles or kilometers
    
    $get_lat = $_GET["lat"];
    $get_lng = $_GET["lng"];
    
    $dbSelect = mysql_query("SELECT  `companies`.`customerName`,
    (2 * (".$earth_radius_miles." * ATAN2(
              SQRT(
                POWER(SIN((RADIANS(".$get_lat." - `locations`.`gps_lat` ) ) / 2 ), 2 ) +
                COS(RADIANS(`locations`.`gps_lat`)) *
                COS(RADIANS(".$get_lat.")) *
                POWER(SIN((RADIANS(".$get_lng." - `locations`.`gps_lng` ) ) / 2 ), 2 )
              ),
              SQRT(1-(
                POWER(SIN((RADIANS(".$get_lat." - `locations`.`gps_lat` ) ) / 2 ), 2 ) +
                COS(RADIANS(`locations`.`gps_lat`)) *
                COS(RADIANS(".$get_lat.")) *
                POWER(SIN((RADIANS(".$get_lng." - `locations`.`gps_lng` ) ) / 2 ), 2 )
              ))
            )
          ))
    AS 'distance'
    FROM  `locations` 
    JOIN  `companies` ON  `locations`.`co_id` 
    HAVING  distance < ".$result_radius."
    ORDER BY distance
    LIMIT 0 , 10") 
    or die(mysql_error());
    

    Results:

    [{"customerName":"SOME COMPANY","distance":"0"}]
    

    I have tested these results with other coordinates and seems to be working perfectly. Haven’t tested anything with great distance between the two points but my application doesn’t require me to do so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do a simple JSON return but I am having issues
I'm trying to return a 2d array from call(), I'm having some issues. My
Having issues trying to select html injected into the DOM using jquery's load. Works
I am having issues trying to get a database connection using the code below:
I'm having issues creating a valid sql query. Im trying to check if a
I am having issues trying to display the data in the console.. i just
I am having issues trying to print a list in python. I want to
I'm trying to convert some VB.net code into C# and having issues trying to
I am having issues with trying to convert an UTF-8 string to unicode. I
I'm having issues when trying to call a MySQL (5.0.77) stored procedure with parameters,

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.