How to implement Spatial(Geo-Location) searching in Grails?
I am working on Grails 1.3.2 with MySql. I need to store the latitude and longitude of certain locations in the database and then on the basis of the user's current location, I need to return the items that are within a particular radius of that location. So, we basically have the following requirements:
- Search for places with-in a given radius of the user's current co-ordinates
- Provide a full text search. We are currently using searchable for this
- Do a combination of full text search with-in a given radius of the user's current co-ordinates.
I have been looking into the various options that we have here and wanted to know what are your views/ suggestions to implement this. The various options that we have here are:
Lucene Spatial Search (http://wiki.apache.org/lucene-java/SpatialSearch) and look into how to use it with searchable
Grails Solr Plugin (http://www.grails.org/plugin/solr). But this does not return domain objects.
Grails Stitches Plugin (http://www.grails.org/plugin/stitches). There is not a lot of documentation except on the author's site (http://www.philliprhodes.com/content/stitches-30-seconds).
MySql spatial extension along with a full text index on all the fields of the domain class. If we go this route, then we will not be using searchab开发者_JAVA技巧le at all.
- Postgres/PostGIS integration with hibernate-spatial (http://blog.mollusca.ch/2008/10/4/grails-spatial-data-postgis)
I believe that this is a very basic requirement in any application that integrates with maps.
So, I am really interested in knowing the most appropriate way to implement this functionality.
Thanks
(I made a prototype using Long/Lat to find rows within a radius, but have now abandoned that in favour of the GridRef system available in the UK)
As a first step, don't use a WHERE condition based on a calculated field (e.g. actual distance based on the given coord and the row's coord).
Instead, try this:
- Imagine the circle where radius = max distance
- Draw a box around it using the curved longitude lines.
- Find items within that box.
In practice this means:
- work out how many degrees longitude represent your maximum distance at the given latitude
- work out how many degrees latitude represent your maximum distance at the given longitude (this multiplier will be fixed when using a spherical-earth model).
- SELECT * FROM places WHERE long > $westside AND long < $eastside AND lat < $northside and lat > $southside
This means you do very simple calculations to get a vague subset covering an area about 30% larger than your actual circle. If you want to add a text search too, either SELECT FROM (subquery) or add your text clause AFTER the simple comparison above, as text searches are computationally expensive.
There is a Grails Spatial plugin. Looks like SOME of its subplugins do support searching, like HDB plugin.
精彩评论