2

我已经在 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);

?>

类.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; 

    }
}
?>

上面的脚本返回以下错误

致命错误:在第 35 行对 D:\xampp_1.8.3\htdocs\GeoMongo\class.master.php 中的非对象调用成员函数 find()

如何解决此错误?

4

1 回答 1

0

将您的收藏设置为:

$this->collection = $this->db->selectCollection($collName);
于 2016-12-16T12:49:34.713 回答