gpt4 book ai didi

javascript - 查找 LatLng Coord 是否位于其他两个 LatLng Coords 之间

转载 作者:行者123 更新时间:2023-11-30 15:57:05 25 4
gpt4 key购买 nike

目标

知道一个坐标点是否在另外两个坐标之间。

背景

我正在制作一个谷歌地图应用程序,我需要知道某个点是否在两个 LatLng 点(起点和终点)之间。

我正在寻找如下函数:

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";
}

我尝试了什么

为了实现这一点,我阅读了几个问题和主题:

代码

无论我尝试什么,我都无法让它发挥作用,但我确实有一个失败实验的最小例子:

"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>

问题

  • 如何使用数学方法确定给定坐标是否介于两个坐标之间?

最佳答案

一个选择是使用 google.maps.geometry.poly.isLocationOnEdge方法。

代码片段:

var map;

function initialize() {

var mapOptions = {
zoom: 2,
center: new google.maps.LatLng(0, -180),
mapTypeId: google.maps.MapTypeId.TERRAIN
};

map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);

var 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)
];

var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: false,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});

flightPath.setMap(map);

var marker = new google.maps.Marker({
position: new google.maps.LatLng(37.772323, -122.214897),
draggable: true,
map: map,
title: 'Drag me!'
});

marker.addListener('dragend', function(event) {

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

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

google.maps.event.addDomListener(window, 'load', initialize);

function checkCoordinate(start, end, point) {
return google.maps.geometry.poly.isLocationOnEdge(point, new google.maps.Polyline({
map: map,
path: [start, end]
}), 10e-1);
}
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>

关于javascript - 查找 LatLng Coord 是否位于其他两个 LatLng Coords 之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38353931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com