Location Search
LBS, which stores the latitude and longitude coordinates of each location, searches for nearby locations and establishes a geographical index to improve query efficiency.
MongoDB geolocation Index, 2d and 2dsphere, corresponds to planar and spherical surfaces.
1. Create lbs collection Storage location coordinates
use lbs; Db.lbs.insert ( { loc:{ type: "point", coordinates: [113.332264, 23.156206] }, name: "Guangzhou East railway 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 Stand"
2. Create a geo-index
Db.lbs.ensureIndex ( { loc: "2dsphere" })
3. Query for nearby coordinates
The current position is: Times Square,
Coordinates: 113.323568, 23.146436
Search nearby points within a kilometer, sorted by near to far
db.lbs. Find ( { loc: {$near: {$geometry:{ type : "Point", coordinates: [113.323568, 23.146436] }, $maxDistance : +} }
Search results:
The PHP code is as follows:
<?PHP//Connect to MongoDBfunctionConn$dbhost,$dbname,$dbuser,$dbpasswd){ $server= ' mongodb://'.$dbuser.‘:‘.$dbpasswd.‘ @‘.$dbhost.‘ /‘.$dbname; Try{ $conn=NewMongoclient ($server); $db=$conn->selectdb ($dbname); } Catch(mongoexception$e){ Throw NewErrorexception (' Unable to connect to DB server. Error: '.$e->getmessage (), 31); } return $db; } //inserting coordinates to MongoDBfunctionAdd$dbconn,$tablename,$longitude,$latitude,$name){ $index=Array(' loc ' = ' 2dsphere '); $data=Array( ' Loc ' =Array( ' Type ' = ' point ', ' coordinates ' =Array(Doubleval($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; } //searching for nearby coordinatesfunctionQuery$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 points 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. Here's how:
Use lbs; DB. CreateUser ( { "user": "Root", "pwd": "123456", "roles":[] }) db. Auth ( { "user": "Root", "pwd": "123456"
Calculate the distance from the two-point geographic coordinates
Function: Calculates the spherical distance between two points according to the latitude and longitude of pi and earth radius coefficients and coordinates of two points.
Get two point coordinate distance:
<?PHP/** calculates the distance between two points of geographic coordinates * @param decimal $longitude 1 beginning Longitude * @param decimal $latitude 1 beginning latitude * @param Decimal $longitude 2 End Longitude * @param Decimal $latitude 2 End latitude * @param int $unit Unit 1: M 2: km * @param Int $decimal precision reserved decimal place * @return Decima L*/functionGetdistance ($longitude 1,$latitude 1,$longitude 2,$latitude 2,$unit=2,$decimal=2){ $EARTH _radius= 6370.996;//radius coefficient of the Earth $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; //End 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?>
PHP Search location and calculate the two-point distance between locations instances