Meteor: Locate the closest transportation option between two locations

I'm currently in the process of developing a carsharing app, and when it comes to setting up rides, I have this kind of data structure stored in my database:

    {
        "_id": "YyPpkCDhTKStGw6CL",
        "authorId": "W6zvbcqit4Mw6a2iK",
        "stages": [
            {
                "caption": "Paris, France",
                "type": "Point",
                "coordinates": [
                    2.3522219000000177,
                    48.856614
                ]
            },
            {
                "caption": "Lyon, France",
                "type": "Point",
                "coordinates": [
                    4.835659,
                    45.764043
                ]
            },
            {
                "caption": "Toulouse, France",
                "type": "Point",
                "coordinates": [
                    1.4442090000000007,
                    43.604652
                ]
            }
        ],
    }

The stages are arranged in the required sequence (Paris -> Lyon -> Toulouse). I have a basic form with two input fields (start and end). My query is: How can I identify the closest ride?

It appears that I need to do something along these lines:

Identify rides where:

  1. stages.X close to start
  2. stages.Y close to end
  3. X < Y

Do you have any thoughts on how I can execute such a search?

Answer №1

I stumbled upon a unique method for getting the job done, so here it is:

// Setting up variables and parameters
var startPoint = [2.3522219000000177, 48.856614];
var endPoint = [4.835659, 45.764043];
var maxDistance = 2000; // measured in meters

// Firstly, locate IDs of rides near my starting point
var rideIds = Rides.find({
    stages: {
        $near: {
            $geometry: {
                type: 'Point',
                coordinates: startPoint,
            },
            $maxDistance: maxDistance,
        }
    }
}, {
    fields: {
        _id: 1
    }
}).map(function(item) { return item._id; });

// Next, find rows close to my destination but within the previously found IDs
var matchedRides = Rides.find({
    _id: { $in: rideIds },
    stages: {
        $near: {
            $geometry: {
                type: 'Point',
                coordinates: endPoint,
            },
            $maxDistance: maxDistance,
        }
    }
});

// Loop through the results to filter out cases where points are in incorrect order
var finalResults = [];
matchedRides.forEach(function(row) {
    var distStart = [];
    var distEnd = [];
    _(row.stages).each(function(stage) {
        distStart.push(calculateDistance(stage.coordinates, startPoint));
        distEnd.push(calculateDistance(stage.coordinates, endPoint));
    });

    var nearestStart = _.indexOf(distStart, _.min(distStart));
    var nearestEnd = _.indexOf(distEnd, _.min(distEnd));

    if (nearestStart < nearestEnd)
        finalResults.push(row);
});

// Displaying the final filtered results
console.log(finalResults);

Here's the function used to calculate distance between two points:

calculateDistance = function(p1, p2) {
    var EARTH_RADIUS = 6378137; // Earth’s mean radius in meters
    var deltaLat = rad(p2[0] - p1[0]);
    var deltaLong = rad(p2[1] - p1[1]);
    var a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
        Math.cos(rad(p1[0])) * Math.cos(rad(p2[0])) *
        Math.sin(deltaLong / 2) * Math.sin(deltaLong / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var distance = EARTH_RADIUS * c;
    return Math.round(distance); // returns the distance in meters
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for printing a page in plain text format on a matrix-dot printer?

I've come across many similar questions on SO about this issue, and I've tried various solutions without success. I'm using an Epson LX300+II printer and I want to print pages from my web application in a plain format, like how it would look ...

Summing arrays of numbers within an HTML form input and showcasing the final result

I have an array with multiple titles such as 'Get 20% Off', 'Receive 40% Savings', 'Grab 10% discounts', etc. My goal is to extract the numeric value before the '%' sign from each title and pass it as an input into a ...

Having difficulty with Angular's ng-options feature in a Select element

I'm currently tackling an issue with using ng-options to iterate through an array of objects and display specific properties in a select element. Upon querying the api/admin endpoint, I receive JSON data containing details of all users holding the ad ...

Using Perl arrays within a PostgreSQL INSERT query

There seems to be a logical issue within my code. In my MongoDB database, I have fields for template, value, row, and column. For example, if $record->{template} is T1, $record->{column} is 1, and $record->{row} contains dates in the format "d.m.Y ...

Customize your Vite logger settings using the config plugin

I've developed a plugin that successfully modifies the Vite configuration of the app it is linked to. However, I'm facing an issue with the custom logger function. Why is this happening? Here's a basic example: export default function custo ...

What would be the best way to create a JavaScript function that emulates the functionality of an Upgrade Button?

I am currently working on developing a clicker game and have reached the point where I need to implement a function for the 'upgrade click' button. This function should deduct a certain amount of money when clicked, while also increasing the amou ...

How can I utilize a custom shader in Three.js to fill a png texture and establish transparency?

I am facing an issue with changing the color of a texture that predominantly contains red. Despite my efforts, the texture is completely filling the picture with red. Can someone please help me identify the error? I am also struggling to understand why I ...

The input value remains a string even after it has been converted to a float

I can't figure out where I'm going wrong. I'm attempting a simple task: getting user input, converting it to a float, and performing a calculation with that value. However, all I keep getting is NaN. Here's the input (I normally replac ...

Initially, my PDF file does not get selected, except in the Internet Explorer browser

I am currently facing an issue with selecting PDF files in Internet Explorer. The process works smoothly in Google Chrome, but I encounter a problem when trying to select PDFs in IE. Specifically, when I attempt to select a PDF for the first time in Inter ...

initiating a submission upon the occurrence of an onchange event on an input field of type "file"

I have encountered an issue while trying to submit a form using the onchange event of an input element with type file. The problem is that it submits an empty form even when a file has been chosen. Here is the code snippet: var form = document.createElem ...

Mastering the Sequence of JS Functions (or How Promises Still Confuse Me)

Let me explain my objective in simple terms: I have a set of functions that must be executed in a specific order, and each function has its own sub-order. This is how my code looks currently: async function LastModuleFunction() { await FirstModuleFun ...

NodeJS refuses to import a file that is not compatible with its structure

My website has two important files: firebase.js gridsome-server.js The firebase.js file contains JavaScript code related to Firebase integration: import firebase from 'firebase/app' import 'firebase/firestore' const config = { ap ...

What is the best way to include a second (concealed) value in a cell using datatables

Query: How can I send specific cell values to a controller using POST method, where one value is visible and the other is hidden? For instance, each cell contains both Time and Date data but only the Time is displayed in the datatable. I need to post both ...

Prioritize establishing the connection before guiding the user to a new destination

I have a brilliant idea for creating something amazing, but I'm uncertain if it's feasible. Here is a basic example of an ajax function that could potentially establish a connection with a server... function getFakePage(userId) { var ajaxObj ...

What is the best way to sum up multiple checkbox values in JavaScript?

var bookRate = new Array('The Right to differ', 'Issues In Contemporary Documentary', 'Writing, Directing and Producing', 'Lee Kuan Yew My Lifelong Challenge'); var selection = document.rate.checkbox; var sum = 0.00 ...

JS problem with using for and foreach loops in Node.js

I've been really stumped by this situation. Everything was running smoothly until 4 days ago when two of my cron daemon jobs suddenly stopped working. Instead of ignoring the issue, I decided to take the opportunity to rebuild and enhance the code. I ...

What strategies can be employed to manage three conflicting versions of a single library within a Vite project?

I am currently facing a unique challenge in my web app development process. Specifically, I am using the Vite framework with the svelte-ts template setup to build an application that integrates different libraries for WebXR based augmented reality. The goa ...

extract the text content from an object

I am trying to add an object to the shopping cart. The item contains a key/value pair as shown in the following image: https://i.stack.imgur.com/5inwR.png Instead of adding the title with its innerText using p and style, I would like to find another ...

Using Cocoon Gem in Rails 5 to create nested forms within nested forms

Currently, I am experimenting with the cocoon gem to construct nested forms efficiently. Within my application, I have defined models for Organisation, Package::Bip, and Tenor. The relationships between these models are as follows: Organisation has_ma ...

Execute the gulp module on the source files

Recently, I've been delving into the world of gulp and trying to enhance the readability of my js source files. I have a task in place (which executes successfully) that utilizes 'gulp-beautify' to beautify the js files: gulp.task('js& ...