Flame_Phoenix - 1 year ago 97
Javascript Question

# Find if a LatLng Coord is between two other LatLng Coords

## Objective

To know if a coordinate point is between two other coordinates.

## Background

I am making a google-maps application, and I need to know if a certain Point is between two LatLng points (start and end).

I am looking for a function like the following:

``````var currentCoord = {lat: 51.8732, lng: -118.6346};
var startCoord = {lat: 61.3434, lng: -118.0046};
var endCoord = {lat: 50.5468, lng: -118.5435};

function isBetween(startCoord, endCoord, currentCoord){
//calculations here
return "true if currentCoord is between startCoord and endCoord, or false otherwise";
}
``````

## Code

No matter what i try, I cannot make it work, but I do have a minimal example with my failed experiments:

``````"use strict";

function initialize() {

let mapOptions = {
zoom: 3,
};

mapOptions);

let flightPlanCoordinates = [
];

path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});

flightPath.setMap(map);

console.log("Marker is over the polyline");
});

draggable: true,
map: map,
title: 'Drag me!'
});

let startPoint = {
lat: 37.772323,
lng: -122.214897
};
let endPoint = {
lat: 21.291982,
lng: -157.821856
};
let currentPoint = {
lat: marker.getPosition().lat(),
lng: marker.getPosition().lng()
};

if (checkCoordinate(startPoint, endPoint, currentPoint))
console.log("in line !");
});
}

function checkCoordinate(start, end, point) {
var slope = (end.lng - start.lng) / (end.lat - start.lat);
var newSlope = (end.lng - point.lng) / (end.lat - point.lat);
return (point.lat > start.lat && point.lat < end.lat && point.lng > start.lng && point.lng < end.lng && slope == newSlope);
}``````

``````html,
body,
#map-canvas {
height: 100%;
margin: 0px;
}``````

``````<!DOCTYPE html>
<html>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple Polylines</title>
<script src="script.js" type="text/javascript"></script>

<body>
<div id="map-canvas"></div>
</body>

</html>``````

## Question

• How can I find out if a given coordinate is between two coordinates using a mathematical approach?

One option would be to use the google.maps.geometry.poly.isLocationOnEdge method.

code snippet:

``````var map;

function initialize() {

var mapOptions = {
zoom: 2,
};

mapOptions);

var flightPlanCoordinates = [
];

path: flightPlanCoordinates,
geodesic: false,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});

flightPath.setMap(map);

draggable: true,
map: map,
title: 'Drag me!'
});

var startPoint = {
lat: 37.772323,
lng: -122.214897
};
position: startPoint,
map: map
});
var endPoint = {
lat: 21.291982,
lng: -157.821856
};
position: endPoint,
map: map
});
var currentPoint = {
lat: marker.getPosition().lat(),
lng: marker.getPosition().lng()
};

if (checkCoordinate(startPoint, endPoint, marker.getPosition()))
console.log("in line !");
});
}

function checkCoordinate(start, end, point) {
map: map,
path: [start, end]
}), 10e-1);
}``````
``````html,
body,
#map-canvas {
height: 100%;
margin: 0px;
}``````
``````<!DOCTYPE html>
<html>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple Polylines</title>