/* Select records by radius (1000 meters) */
SELECT * from Pointlocation where Earth_box (Ll_to_earth (40.057031,116.307852), 4000) @> Ll_to_earth ( Pointlocation.lat, Pointlocation.lon);
/* The distance between two points is awarded */
SELECT earth_distance (Ll_to_earth (40.057031,116.307852), Ll_to_earth (Pointlocation.lat, Pointlocation.lon)) from Pointlocation where id=1112
- /*
- * PostgreSQL's earthdistance study notes
- * Author:wusuopubupt
- * date:2013-03-31
- */
- /* CREATE TABLE */
- CREATE TABLE Picture (
- ID serial PRIMARY KEY,
- P_uid char () not NULL,
- P_key char (at) not NULL,
- Lat real not null,
- LNG real not NULL,
- the up int is not NULL,
- the down int is not NULL,
- IP varchar (+) DEFAULT NULL,
- Address varchar (DEFAULT) NULL
- );
- /* Insert Record */
- INSERT into Picture (P_uid, P_key, lat, LNG, up, down, IP, address)
- VALUES (' AAAABBBBCCCC ', ' 2014032008164023279.png ', 40.043945, 116.413668, 0, 0, ' , ');
- /* Insert Record */
- INSERT into Picture (P_uid, P_key, lat, LNG, up, down, IP, address)
- VALUES (' xxxxccccmmmm ', ' 2014032008164023111.png ', 40.067183, 116.415230, 0, 0, ' , ');
- /* Select Record */
- SELECT * from Picture ;
- /* Update record */
- UPDATE picture SET address=' Lishuiqiao ' WHERE id=1;
- UPDATE picture SET address=' Tiantongyuan ' WHERE id=2;
- /* Create an index on the latitude column */
- CREATE INDEX ll_idx on the picture USING gist (Ll_to_earth (lat, LNG));
- /* Select records by radius (1000 meters) */
- SELECT * from the picture where Earth_box (Ll_to_earth (40.059286,116.418773),,) @> Ll_to_earth (Picture.lat, PICTURE.LNG);
- /* Select the distance from the current user */
- SELECT picture.id, Earth_distance (Ll_to_earth (Picture.lat, PICTURE.LNG), Ll_to_earth (40.059286,116.418773))
- As Dis from picture
- ORDER by dis ASC;
- /*
- * The following is a tutorial on the Web
- * Address: Http://www.cse.iitb.ac. in/dbms/data/courses/cs631/postgresql-resources/postgresql-9.2.4/contrib/earthdistance/expected/ Earthdistance. out
- */
Geo-coordinate calculation