我在MongoDB中有一个名为restaurants的集合
{
"_id" : ObjectId("5281f8d660ad39040c000001"),
"name" : "Bucksters Coffee",
"serves" : "Fast Food",
"location" : [
"23.755339",
"90.375408"
]
}
{
"_id" : ObjectId("5285cf0860ad39380b000000"),
"name" : "A1 Donuts",
"serves" : "Fast Food",
"location" : [
"18.5087016",
"73.8124984"
]
}
{
"_id" : ObjectId("5285cf3f60ad39380b000002"),
"name" : "B1 Donuts",
"serves" : "Fast Food",
"location" : [
"18.4893148",
"73.8213213"
]
}
{
"_id" : ObjectId("5285e7a260ad39380b000009"),
"name" : "C1 Donuts",
"serves" : "Fast Food",
"location" : [
"18.5308225",
"73.8474647"
]
}
我的位置是[“18.5170345”,“73.83476”],我想找到离我所在地约5公里的最近的餐馆。
所以我尝试了一些事情,
用于插入餐馆,
<?php
try {
$conn = new Mongo('localhost');
// access database
$db = $conn->demo;
// access collection
$collection = $db->restaurants;
$forlatadd="shivaji nagar, pune";
//for lat& Long
$prepAddr = str_replace(' ','+',$forlatadd);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$a=array("$lat","$long");
//print_r($a);
$document = array( "name" => "C1 Donuts","serves" => "Fast Food","location" =>$a);
$collection->insert($document);
$collection->ensureIndex(array("location" => "2d"));
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
为了搜索数据,
<?php
try {
$conn = new Mongo('localhost');
// access database
$db = $conn->demo;
// access collection
$collection = $db->restaurants;
$forlatadd="deccan, pune";
//for lat& Long
$prepAddr = str_replace(' ','+',$forlatadd);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$a=array("$lat","$long");
//print_r($a);
$distance='5000';
$query = array('location' => array('$near' => array($lat,$long),'$maxDistance' => intval($distance)));
$cursor = $collection->find($query);
if ($cursor) {
echo json_encode(iterator_to_array($cursor));
} else {
echo "{ 'status' : 'false' }";
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
但这会产生以下错误
Error: localhost:27017: can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { location: { $near: [ 18.5170345, 73.83476 ], $maxDistance: 5000 } }
但我已经完成了
$collection->ensureIndex(array("location" => "2d"));
在收集新餐馆时, 请帮我从我所在的地方找到最近的餐馆。
答案 0 :(得分:1)
它为我工作,
只有改变,
$a=array("$lat","$long"); //It takes lat & long as string
到
$a=array($long,$lat); //It takes lat&long as float.
及其工作
答案 1 :(得分:0)
在mongodb文档中,他们在位置定义中指定顺序,事实上我在上一个开发中的订单有问题:
重要按此顺序指定坐标:“经度,纬度。”
http://docs.mongodb.org/manual/reference/operator/query/near/