I am creating NodeJS backend for uber like app for android and iOS. locations of users will be taken from phone and coordinates i.e. latitude and longitude will be sent to server. There are 2 groups of users. lets say drivers and passengers. once passenger press search for driver, I need to send notifications to all the drivers with in 5 km of the passenger radius. Any ideas how to implement this logic .??. On frontend geo fences will be created around the passengers. what I cant figure out is that how to know on the server side if drivers are in those geo fences or not. Frontend is also being developed by me using react native so any suggestions from frontend point of view are also welcome. Thanks
MongoDB support Geospatial queries, with that you can query document in the specified geolocation bounds.
Assume that we have drivers
collection as below
db.drivers.insertMany( [
{
name:"Patrick Konrab",
location: { type: "Point", coordinates: [ -73.97, 40.77 ] },
},
{
name: "James Webb",
location: { type: "Point", coordinates: [ -73.9928, 40.7193 ] },
category: "Parks"
},
{
name: "Kyle Grill",
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
}
] )
The following operation creates a 2dsphere
index on the location field:
db.drivers.createIndex( { location: "2dsphere" } )
The drivers
collection above has a 2dsphere
index. The following query uses the $near
operator to return documents that are at least 1000 meters
from and at most 5000 meters
from the specified GeoJSON point, sorted in order from nearest to farthest:
// Passenger coordinates should be returned user device
//https://github.com/react-native-geolocation/react-native-geolocation
const passengerCoordinate = [ -73.9667, 40.78 ]
db.drivers.find(
{
location:
{ $near:
{
$geometry: { type: "Point", coordinates: passengerCoordinate },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)