Question

I have developed an android app which marks some information with google map api.

But I don't have an idea for an algorithm of searching closest points compared with custom DB coordinates.

Here is a hypothesis.
1) The DB server has 1,000 data.
2) Each 1,000 data have a coordinate[lati & longi] of a some nation.
3) The app shows & set markers of top 20 nearest points, compared with current users coordinate.

I have two simple ideas for solving this problem. but both of them are not effective.

A) Look for all of data in DB. then compare, pick top 20 points.
; it must select whole 1,000 data...

B) Insert a detail address categories into each 1,000 data. (state, city, post# & etc..) then get detail address of current location, and compare only the specified address categories. ; looks good but, there's a boundary problem. if you are at a place between "A" city & "B" city, it only find a points of "A"s or "B"s.

Is there any solutions or ideas?

Was it helpful?

Solution

Your best bet is to put the data in a spatial database such as PostGIS, and execute a spatial query that performs a nearest neighbour search.

From http://boundlessgeo.com/2011/09/indexed-nearest-neighbour-search-in-postgis/:

PostGIS (the development code in the source repository) now has the ability to do index-assisted nearest neighbour searching... You will need PostgreSQL 9.1 and the PostGIS source code from the repository, but this is what a nearest neighbour search looks like:

   SELECT name, gid
   FROM geonames
   ORDER BY geom <-> st_setsrid(st_makepoint(-90,40),4326)
   LIMIT 10;

Note the magic <-> operator in the ORDER BY clause. This is where the magic occurs. The <-> is a “distance” operator, but it only makes use of the index when it appears in the ORDER BY clause. Between putting the operator in the ORDER BY and using a LIMIT to truncate the result set, we can very very quickly (less than 10ms on a 2M record table, in this case) get the 10 nearest points to our test point.

There are spatial databases for Android such as Spatialite if you require the data to be stored locally.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top