我在PHP中尝试了Mongo地理空间查询..我遇到了以下错误我已经写了2个PHP脚本..在我的 GeoPHP 从类创建对象并调用成员方法和我的另一个PHP脚本 class.master.php 写了一个类和成员方法
GeoPHP.php
<?php
require 'class.master.php';
$lat = 27.53864605;
$long = 94.57980515;
$distance = 5;
$geObj = new Geo;
$geObj->DbInst('dbname', 'collectionname');
$geObj->assignGeo($lat, $long, $distance);
$resultFld = array('id', 'name', 'stid', 'dis_id', 'ta_id');
$query = array('location'=>
array("\$near"=>
array("\$geometry"=>
array('type'=>'Point', 'coordinates'=>
array($long, $lat)),"\$minDistance"=> 0, "\$maxDistance"=>$distance
)
)
);
$result = $geObj->getResult($query, $resultFld);
?>
class.master.php
<?php
require 'vendor/autoload.php';
class Geo{
private $latitude, $longitude, $distance;
private $uri = "mongodb://localhost:27017";
private $databaseName;
private $db;
public $collection;
public function DbInst($dbname, $collName){
// Mongo Connection Establish
$con = new MongoDB\Client($this->uri);
$this->databaseName = $dbname;
$this->db = $con->selectDatabase($this->databaseName);
$this->collection = $collName;
}
public function assignGeo($lat, $long, $dis){
$this->latitude = $lat;
$this->longitude = $long;
$this->distance = $dis;
}
public function getResult($query, &$resFld){
$cursor = $this->collection->find($query);
return $cursor;
}
}
?>
上面的脚本返回错误
致命错误:在非对象中调用成员函数find() 第35行的D:\ xampp_1.8.3 \ htdocs \ GeoMongo \ class.master.php
如何解决此错误?
答案 0 :(得分:0)
将您的收藏集设为:
$this->collection = $this->db->selectCollection($collName);