相關文章:PHP實作附近的人、按距離排序之Redis GEO方案
前一篇文章介紹了使用Redis GEO實作附近的人功能,但是留下了諸多弊端,是以要換一種方案,使用MongoDB存儲使用者的位置資訊,實作附近的人功能,這裡推薦使用這一種方案。
首先伺服器需要安裝MongoDB軟體以及php的mongo_db擴充,這裡不做介紹。然後封裝一個php操作的MongoDB的單例類,可以參考文章:PHP封裝一個MongoDB單例類
1.更新使用者位置資訊時存入MongoDB,示例如下:
<?php
namespace app\index\service;
use app\index\cache\Mongo;
class NearbyService extends Model
{
/**
* 使用者上傳位置資訊
* @param int $user_id 使用者id
* @param string $latitude 緯度
* @param string $longitude 經度
* @return object
*/
public function subSite($user_id,$latitude,$longitude){
$user_id = intval($user_id);
$collname = 'user_location';
//查詢
$sel =