简体   繁体   English

如何使用Laravel雄辩的经纬度从mongodb获取位置?

[英]How to fetch location from mongodb based on latitude and longitude using Laravel eloquent?

I am using Moloquent model for fetching data from MongoDB in Laravel. 我正在使用Moloquent模型从Laravel中的MongoDB获取数据。

Everything was fine except getting data based on latitude and longitude. 除了获取基于纬度和经度的数据外,其他一切都很好。 This is how my collection looks: 这是我的收藏集的外观:

{
   "_id" : "some_id",
   "place_id" : "353789030392849490",
   "latitude": "22.5726",
   "longitude" : "88.3639"
}

In MySQL and Laravel Eloquent, we can fetch data using latitude and longitude like this: 在MySQL和Laravel Eloquent中,我们可以使用纬度和经度来获取数据,如下所示:

DB:select("SELECT id, (
    6371 * acos( cos( radians(37) ) * cos( radians( lat ) ) 
    * cos( radians( long ) - radians(-122) )
    + sin( radians(37) ) * sin(radians(lat)) )
) AS distance 
FROM myTable
HAVING distance < 50
ORDER BY distance")->get();

Can I do the same using Laravel Eloquent to fetch data from MongoDB? 我可以使用Laravel Eloquent从MongoDB获取数据吗? Or do I need to do it in some other way? 还是我需要以其他方式做到这一点?

PS. PS。 I am new to MongoDB. 我是MongoDB的新手。

You can do it like the following as stated in the README.md of laravel-mongodb : 您可以按照laravel-mongodbREADME.md中所述的README.md进行操作

$users = User::where('location', 'near', [
    '$geometry' => [
        'type' => 'Point',
        'coordinates' => [
            -0.1367563,
            51.5100913,
        ],
    ],
    '$maxDistance' => 50,
]);

NOTE: Specify coordinates in this order: longitude, latitude. 注意:请按以下顺序指定坐标:经度,纬度。

Your collection should have a 2dsphere index: https://docs.mongodb.com/manual/core/2dsphere/ 您的集合应具有2dsphere索引: https : 2dsphere

An example of a collection: 集合示例:

{
    loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
    name: "Central Park",
    category : "Parks"
}

For more information about the $near : https://docs.mongodb.com/manual/core/2dsphere/ 有关$near更多信息: https : //docs.mongodb.com/manual/core/2dsphere/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用laravel / php基于经度/纬度接近度对对象进行分组 - How to group objects based on longitude/latitude proximity using laravel/php 如何使用Laravel播种机中的Faker生成纬度,经度信息? - How to generate latitude, longitude info using Faker from Laravel seeder? 如何在laravel中搜索给定距离和给定纬度和经度的所有位置 - how to search all the location for given distance and given latitude and longitude in laravel 根据经度和纬度显示地图时如何更新地图,即; 从数据库中获取 - How to Update Map when Map show based on longitude and latitude i.e; fetch from database 如何使用 eloquent ORM 根据 Laravel 中的 post ip 地址从表中获取记录 - How to fetch record from table based on post ip address in laravel using eloquent ORM 如何使用PHP JSON对象从Google Maps API获取5个附近地点的经度和纬度? - How to fetch Latitude and Longitude of 5 Nearby Places from Google Maps API using PHP JSON object? 如何使用html在php中发布我所在位置的经度和纬度? - How to post the latitude and longitude of my location in php using html? 如何使用谷歌地图查找当前位置(经度和纬度)? - How to find the current location(longitude and latitude) using Google Map? 如何从MySQL的特定位置选择经度,纬度? - How to select longitude, latitude around specifc location from mysql? 如何使用Laravel 5.7中的Cron Job将纬度和经度从zipcode更新到数据库 - How to update latitude and longitude from zipcode to the database using Cron Job in Laravel 5.7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM