gpt4 book ai didi

php - 使用 mongodb 和 php 查找最近的餐厅位置

转载 作者:可可西里 更新时间:2023-11-01 10:44:05 26 4
gpt4 key购买 nike

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

在集合中插入新餐厅时,请帮我找到离我最近的餐馆。

最佳答案

它对我有用,

只改变,

$a=array("$lat","$long");  //It takes lat & long as string

$a=array($long,$lat);  //It takes lat&long as float.

及其工作

关于php - 使用 mongodb 和 php 查找最近的餐厅位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20001467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com