Beware this content is over 6 years old and considered stale. It may no longer be accurate and/or reflect the understanding of the author but remains here for reference only. Please keep this in mind as you use this content.
I’ve been doing a little geocoding experiments with some of the re-released TfL data that you can get your hands on.
With PHP and Microsoft SQL Server (MSSQL), the solution is almost the same:
$query = "SELECT TOP $this->MapProperties *,(((acos(sin(($this->MapLatitude*pi()/180)) * sin((WGS84Latitude*pi()/180))+cos(($this->MapLatitude*pi()/180)) * cos((WGS84Latitude*pi()/180)) * cos((($this->MapLongitude - WGS84Longitude)*pi()/180))))*180/pi())*60*1.1515".($this->MapDistanceKM ? '*1.609344' : '').") AS Distance FROM $this->DB_TABLE WHERE WGS84Latitude IS NOT NULL AND WGS84Longitude IS NOT NULL AND WGS84Latitude != 0 ORDER BY Distance";