# Getting coordinates falling inside two known points using javascript

I'm working on a solution, where a parent track the vehicle in which his kids are travelling. Initially I will be getting all the geo fence data from the system (Like stopings) and store it in an array, and I'll also be getting the current position of the vehicle every 1 minute.

Now I want to find out which is the next stop or geofence approaching in any given time, considering current position as origin and the parent location as destination, need to find out which is the next stop inbetween those two points?

These are the ideas on which I worked on

1. Calculate the distance from the current position to all the geo fence data and select the short distance. But it did not work out because the next stop can be far from the previously crossed geofences, so that I only get the previous geofence not the next

2. Using 2 points I calculated the heading angle, but with this I couldn't find the next stop

3. Finally I stubbled upon a usable solution, which is to use intersection based on 2 positions, calculating the geofences between lat (min), lat(max) and lng(min) and lng(max). Then finding the distance from the current point and sorting to find the next stop.

But this solution failed because, when the vehicle route is Zigzag the fence data can be outside the bounder of origin & destination points

Below is the images on this solution