Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using just <code>SELECT * FROM Table WHERE lat between $minlat and $maxlat</code> will not be accurate enough.</p> <p>The correct way to query distance is using the coordinates in radians.</p> <pre><code>&lt;?php $sql = "SELECT * FROM Table WHERE acos(sin(1.3963) * sin(Lat) + cos(1.3963) * cos(Lat) * cos(Lon - (-0.6981))) * 6371 &lt;= 1000"; </code></pre> <p>Here is a handy reference - <a href="http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates" rel="nofollow">http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates</a></p> <p>For example: </p> <pre><code>&lt;?php $distance = 100; $current_lat = 1.3963; $current_lon = -0.6981; $earths_radius = 6371; $sql = "SELECT * FROM Table T WHERE acos(sin($current_lat) * sin(T.Lat) + cos($current_lat) * cos(T.Lat) * cos(T.Lon - ($current_lon))) * $earths_radius &lt;= $distance"; </code></pre> <p>And if you want to do the order by and show the distance:</p> <pre><code>&lt;?php $distance = 100; $current_lat = 1.3963; $current_lon = -0.6981; $earths_radius = 6371; $sql = "SELECT *, (acos(sin($current_lat) * sin(T.Lat) + cos($current_lat) * cos(T.Lat) * cos(T.Lon - ($current_lon))) * $earths_radius) as distance FROM Table T WHERE acos(sin($current_lat) * sin(T.Lat) + cos($current_lat) * cos(T.Lat) * cos(T.Lon - ($current_lon))) * $earths_radius &lt;= $distance ORDER BY acos(sin($current_lat) * sin(T.Lat) + cos($current_lat) * cos(T.Lat) * cos(T.Lon - ($current_lon))) * $earths_radius DESC"; </code></pre> <p>Edited for @Blazemonger and the avoidance of doubt :) If you want to work in degrees instead of radians:</p> <pre><code>&lt;?php $current_lat_deg = 80.00209691585; $current_lon_deg = -39.99818366895; $radians_to_degs = 57.2957795; $distance = 100; $current_lat = $current_lat_deg / $radians_to_degs; $current_lon = $current_lon_deg / $radians_to_degs; $earths_radius = 6371; $sql = "SELECT *, (acos(sin($current_lat) * sin(T.Lat) + cos($current_lat) * cos(T.Lat) * cos(T.Lon - ($current_lon))) * $earths_radius) as distance FROM Table T WHERE acos(sin($current_lat) * sin(T.Lat) + cos($current_lat) * cos(T.Lat) * cos(T.Lon - ($current_lon))) * $earths_radius &lt;= $distance ORDER BY acos(sin($current_lat) * sin(T.Lat) + cos($current_lat) * cos(T.Lat) * cos(T.Lon - ($current_lon))) * $earths_radius DESC"; </code></pre> <p>You could easily wrap this up into a class that accepted Radians or Degrees from the information provided above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload