mysqlgeolocationgeometrycartesian-coordinatesgreat-circle

MySQL radius search


I am making an application with radius search, here is the example I learned from

https://developers.google.com/maps/articles/phpsqlsearch_v3#findnearsql

In my case

I do

SELECT (3959 * acos(cos(radians(-37.814107)) * cos(radians(lat)) * cos(radians(lng) - radians(144.96327999999994)) + sin(radians(-37.814107)) * sin(radians(lat)))) AS distance;

when lat = -37.8141070000, lng = 144.9632800000

SELECT (3959 * acos(cos(radians(-37.814107)) * cos(radians(-37.8141070000)) * cos(radians(144.9632800000) - radians(144.96327999999994)) + sin(radians(-37.814107)) * sin(radians(-37.8141070000)))) AS distance;

MySQL output the distance as NULL, why? Did I do something wrong?

How I fix it? Sorry I am really NOT good at math.


Solution

  • Basic SQL query

    SELECT * FROM table
    

    You must have a MySql table in your query with lat & lng as fields. ie

    SELECT (3959 * acos(cos(radians(-37.814107)) 
           * cos(radians(lat)) * cos(radians(lng) - radians(144.96327999999994))
            + sin(radians(-37.814107))
            * sin(radians(lat)))) AS distance FROM table;