目标
判断一个坐标点是否在两个其它的坐标点之间。
背景
我正在制作一个谷歌地图应用程序,需要知道一个特定的点是否在两个经纬度(起点和终点)之间。
我正在寻找下面这样的函数:
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";
}
我尝试过的方法
为了达到这个目的,我阅读了几篇问题和帖子:
- check coordinate exists between two points of line 看起来很有前途,但由于它只针对网格中的X和Y点,并且没有考虑地球曲率和其他方面,所以它不起作用。而且,它已经关闭了...
- Check coordinate point between two points 可能也是我在寻找的东西,但是目前还没有答案,而且问题中的代码也无法工作。如果可能的话,我也想避免一连串的
if
语句。 - Check if a point projected on a line segment is not outside it 也是我检查过的内容,但是不幸的是目标是不同的,我无法将答案调整为我所寻找的内容。
- 最后,http://www.movable-type.co.uk/scripts/latlong.html 是一个坐标资产的巨大库,但我无法看到它们中的任何一个如何帮助我
代码
无论我尝试什么,我都无法让它工作,但是我有一个带有我的失败实验的最小示例:
"use strict";
/*global google*/
function initialize() {
let mapOptions = {
zoom: 3,
center: new google.maps.LatLng(0, -180),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
let map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
let flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431),
new google.maps.LatLng(-27.46758, 153.027892)
];
let flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
google.maps.event.addListener(flightPath, 'mouseover', function(event) {
console.log("Marker is over the polyline");
});
let marker = new google.maps.Marker({
position: new google.maps.LatLng(37.772323, -122.214897),
draggable: true,
map: map,
title: 'Drag me!'
});
marker.addListener('drag', function(event) {
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 !");
});
}
google.maps.event.addDomListener(window, 'load', initialize);
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;
padding: 0px
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple Polylines</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>
<script src="script.js" type="text/javascript"></script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
问题
- 如何通过数学方法确定一个给定坐标是否在两个坐标之间?
PolyUtil.isLocationOnPath
方法。https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java - antonio