How to build your own geographical based search engine
Our goal: provide to our website users the possibility to find all items (cinemas, restaurants, hotels...) within a defined range of their current locations, sorting results upon distance from them.
What we need: a database containing the latitude and longitude of the items, an interface to allow users to enter their current location (it can be a raw form or an interactive map, as you prefer), various software depending on your next choice.
Now we have at least 2 ways to reach our goal:
- Sql query:
it's the easier method, we already have data in our DB, we need only to execute a relatively simple query with a sub-query. Let's see an example in detail:
SELECT item_name, SQRT(POW((@user_lat - lat) * 111319.5 * COS(RADIANS(@user_lng)),2) + POW((@user_lng - lng)*110946.3,2)) as distance
from ( select item_name, lat, lng
from items
where lat >= (@user_lat - @range_degrees/2)
and lat <= (@user_lat + @range_degrees/2)
and lng >= (@user_lng - @range_degrees/2)
and lng <= (@user_lng + @range_degrees/2)
) as boundary
WHERE distance < @range_meters
ORDER by distance;
Using this formula distance is given in meters, and you need to convert user choosen range in meters and degrees. Values 111319.5 and 110946.3 are based on this information.
Patrick O'Leary in his article on GIS based search use an Euclidian formula,I don't know if it's similar or it returns distance in miles etc., I haven't tried it so pay attention when you use his or my formula, do some testing!
- Lucene:
Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java.
Local Lucene is an extension of Lucene providing an implementation of geographical searching. Another similar implementation is GeoLucene but it's still an earlier alpha and his developer is unable to work on this project. Both these software are faster on geographic queries than the unmodified Lucene and for sure faster than Zend_Search_Lucene too.
If you can use Java I recommend to choose Local Lucene, in his whitepaper pjaol shows a comparison beetween Mysql, Lucene and LocalLucene. Instead if you want to use the search capabilities of Lucene in PHP, you can use Solr and, in example, a client like this SolPHP or obtain a JSON response from the server. In this case you can use LocalSolr that is a porting of the LocalLucene library onto the Solr search server.
On GISSearch you can find all information needed to easily build your fast, scalable and reliable geographical search engine.
I hope it will be helpful to you and to your users.