The best way to get the nearest geo-integration (lat / long) to one specified by the user, I have a list of all geo-information stored in the MySQL database

I am trying to create a mobile application that will capture the user's current geodata (lat / long) by sending it to the web service, and the web service will return the 5 closest geotomes from the long list. What I don’t have is how I can get the 5 closest geo-integrals (my initial approach was to get all the points from the database and then calculate the distance between each geodata and the geothermal user and provide the closest to it 5 ) not so expensive process. Any suggestion on how I can do this? If you need more information, let me know so that I can reprint.

+3
source share
3 answers

There are several ways to do this, and how optimally you need it will probably depend on the number of points you have (so it scales well).

For spatial queries, you should use a database that supports spatial indexing, which can significantly speed up the search. PostGIS is one example, and there are GIS extensions for MySQL http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html .

Here is an example of solving the k-nearest neighbor problem in PostGIS http://www.bostongis.com/?content_name=postgis_nearest_neighbor_generic

EDIT: , geohash (http://en.wikipedia.org/wiki/Geohash), .

+1

esperanto.de SQL ( PHP):

$demando =
  "SELECT DISTINCT id, [...]" .
  "   AVG(tie.longitudo) AS longitudo, AVG(tie.latitudo) AS latitudo, ".
  "   ROUND(111.3 *  SQRT( POW(({$chi[latitudo]} - AVG(tie.latitudo)),2) + " .
  "                        POW((({$chi[longitudo]} - AVG(tie.longitudo)) * COS(({$chi[latitudo]} + AVG(tie.latitudo)) / 114.6 )),2) )) " .
  "       AS efektiva_distanco " .
  " FROM MA_Adresoj, gxustaj_plzkoord AS tie " .
  " WHERE ( " . $pliaj_restriktoj . ") " .
  "  and (tie.plz = urba_kodo) " .
  " GROUP BY id, urba_kodo, nomo_pers, nomo_fam, adresaldono, strato, loko," .
  "          telefono, retadreso " .
  " HAVING efektiva_distanco < '" . $distanco . "' " .
  " ORDER BY " . $ordo;

($distanco). , ( , .)

- ( ) :

111.3 *  SQRT( POW(here.latitude - there.latitude),2) +
               POW((here.longitude - there.longitude) * COS( (here.latitude + there.latitude) / 114.6 ),2) ))

( - .)

, - ( ), , , , ... ).

, , , - , iluxas, .

0
select x, y, sqrt((x-userX)^2, (y-userY)^2) as distance from points
where x > userX - 10 and x < userX + 10
and y > userY - 10 and y < userY + 10
order by distance asc
limit 5

the syntax is very approximate of course

-1
source

All Articles