Location Search
LBS, store latitude and longitude coordinates for each location, search for nearby locations, and establish a geographical index to improve query efficiency.
MongoDB Geographical Index, 2d and 2dsphere, corresponding plane and spherical surface.
1. Create lbs set storage location coordinates
use lbs;
Db.lbs.insert (
{
loc:{
type: "point",
coordinates: [113.332264, 23.156206]
},
name: "Guangzhou East station"
}
)
Db.lbs.insert (
{
loc:{
type: "point",
coordinates: [113.330611, 23.147234]
},
name: "Forest and West"
}
)
Db.lbs.insert (
{
loc:{
type: "point",
coordinates: [113.328095, 23.165376]
},
name: "Balance Rack"
}
)
2. Create a geographical index
Db.lbs.ensureIndex (
{
loc: "2dsphere"
}
)
3. Query the coordinates near
The current location is: Times Square,
Coordinate:
Search for points within a kilometer, sorted from near to far
Db.lbs.find
{
loc: {$near: {
$geometry: {
type: "point",
coordinates: [113.323568, 23.146436]
},
$maxDistance: 1000
}
}
)
Search results:
Copy Code code as follows:
{"_id": ObjectId ("556a651996f1ac2add8928fa"), "loc": {"type": "Point", "coordinates": [113.330611, 23.147234]}, " Name ": Forest and West"}
The PHP code is as follows:
<?php//Connection MONGODB function conn ($dbhost, $dbname, $dbuser, $dbpasswd) {$server = ' mongodb://'. $dbuser. $dbpas SWD. ' @ '. $dbhost. '
/'. $dbname;
try{$conn = new Mongoclient ($server);
$db = $conn->selectdb ($dbname); The catch (Mongoexception $e) {throw new errorexception (' Unable to connect to DB server. Error: '.
$e->getmessage (), 31);
return $db; //Insert coordinates to MongoDB function add ($dbconn, $tablename, $longitude, $latitude, $name) {$index = array (' loc ' => ' 2dsphe
Re '); $data = Array (' loc ' => array (' type ' => ' point ', ' coordinates ' => array ($
Longitude), Doubleval ($latitude))), ' name ' => $name);
$coll = $dbconn->selectcollection ($tablename);
$coll->ensureindex ($index);
$result = $coll->insert ($data, Array (' W ' => true)); Return (Isset ($result [' OK ']) &&!empty ($result [' OK ']))?
True:false; //Search Nearby coordinates function query ($dbconn, $tablenamE, $longitude, $latitude, $maxdistance, $limit =10) {$param = array (' loc ' => Array (' $nearSphere ' => Array (' $geometry ' => array (' type ' => ' point ', ' coordinates ' => array doubleval ($
Longitude), Doubleval ($latitude)), ' $maxDistance ' => $maxdistance *1000));
$coll = $dbconn->selectcollection ($tablename);
$cursor = $coll->find ($param);
$cursor = $cursor->limit ($limit);
$result = Array ();
foreach ($cursor as $v) {$result [] = $v;
return $result;
$DB = conn (' localhost ', ' lbs ', ' root ', ' 123456 ');
Randomly insert 100 coordinate records for ($i =0 $i <100; $i + +) {$longitude = ' 113.3 '. Mt_rand (10000, 99999);
$latitude = ' 23.15 '. Mt_rand (1000, 9999);
$name = ' name '. Mt_rand (10000,99999);
Add ($db, ' lbs ', $longitude, $latitude, $name);
//Search for a point within a kilometer $longitude = 113.323568;
$latitude = 23.146436;
$maxdistance = 1; $result = Query ($db, ' lbs ', $longitUde, $latitude, $maxdistance);
Print_r ($result);
?>
To demonstrate PHP code, you first need to create users and execute auth in MongoDB's lbs. The method is as follows:
use lbs;
Db.createuser (
{
"user": "Root",
"pwd": "123456",
"roles": []
}
)
Db.auth (
{
"user": "Root",
"pwd": "123456"
}
)
Calculate the distance of two-point geographical coordinates
function: Calculate the spherical distance between two points according to the longitude and latitude of pi and earth radius coefficient and two point coordinate.
Get two-point coordinate distance:
<?php/** * Calculates the distance between two-point geographical coordinates * @param decimal $longitude 1 beginning Longitude * @param decimal $latitude 1 beginning latitude * @param decimal $lon
Gitude2 End Longitude * @param Decimal $latitude 2 End latitude * @param Int $unit Unit 1: M 2: km * @param int $decimal precision reserved decimal places * @return Decimal */function Getdistance ($longitude 1, $latitude 1, $longitude 2, $latitude 2, $unit =2, $decimal =2) {$EAR Th_radius = 6370.996;
The earth radius coefficient $PI = 3.1415926;
$radLat 1 = $latitude 1 * $PI/180.0;
$radLat 2 = $latitude 2 * $PI/180.0;
$radLng 1 = $longitude 1 * $PI/180.0;
$radLng 2 = $longitude 2 * $PI/180.0;
$a = $radLat 1-$radLat 2;
$b = $radLng 1-$radLng 2;
$distance = 2 * ASIN (SQRT (POW) (sin ($a/2), 2) + cos ($radLat 1) * cos ($radLat 2) * POW (sin ($b/2), 2));
$distance = $distance * $EARTH _radius * 1000;
if ($unit ==2) {$distance = $distance/1000;
Return round ($distance, $decimal);
///start coordinates $longitude 1 = 113.330405;
$latitude 1 = 23.147255;
The endpoint coordinates $longitude 2 = 113.314271;
$latitude 2 = 23.1323; $distance = Getdistance ($longitude 1, $latitude 1, $longitude 2, $latitude 2, 1); echo $distance. ' m ';
2342.38m $distance = getdistance ($longitude 1, $latitude 1, $longitude 2, $latitude 2, 2); Echo $distance. ' Km ';
2.34km?>