【发布时间】:2012-01-20 11:56:30
【问题描述】:
我有一个名为 city 的 mongo 集合,具有以下 json
{
"city_name": "Amesbury",
"lat": "42.8583925",
"lng": "-70.9300376",
"geo_coords": [-70.9300376,42.8583925]
}
如您所见,此收藏的地理位置位于埃姆斯伯里。现在我正在尝试使用 php 获取它,我使用了以下代码。
$collection = $this->mongodb->city;
$collection->ensureIndex(array('geo_coords' => '2d'));
$radiusMiles = 500000; //get all results within 5 miles
$radiusOfEarth = 3956; //avg radius of earth in miles
$cursor = $collection->find(
array('geo_coords' =>
array('$within' =>
array('$centerSphere' =>
array(
array($lng, $lat), $radiusMiles/$radiusOfEarth
)
)
)
)
);
对于此代码,我将波士顿的经度和纬度作为消息参数传递,并且我希望查询获取 amesbury 作为结果,因为它们都是相邻的城市,并且肯定彼此相距 500000 英里。
但是我的查询没有返回任何结果。问题可能是什么?
【问题讨论】:
-
你在 mongo javascript shell 中试过了吗?
标签: php mongodb geolocation