我有两个坐标,我想为其绘制一条等长的垂直线。是否有一个简单的谷歌地图偏移量或一个干净的 javascript 方法,我可以通过它来完成这个?那会是什么?
这是我目前所拥有的。如您所见,我将这两个点绘制为标记,然后尝试在它们之间画一条线,但我需要使该线垂直于两个坐标之间的线。
var locations = [
['', position.coords.latitude, position.coords.longitude, 1],
['', llat, llng, 2]
];
var marker, i;
for ( var i = 0; i < locations.length; i++ )
{
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
}
var borderPlanCoordinates = [
new google.maps.LatLng(llat, position.coords.longitude),
new google.maps.LatLng(position.coords.latitude,llng)
];
var borderPath = new google.maps.Polyline({
path: borderPlanCoordinates,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 10,
map: map
});
borderPath.setMap(map);
最佳答案
所以你有两个坐标为 (x1,y1)
和 (x2, y2)
的点,你想画一条线段,其长度是它们之间的距离, 哪个是连接它们的线段的垂直平分线,哪个被该线段平分?
可能最简单的方法是设置cx = (x1 + x2)/2
, cy = (y1+y2)/2)
, dx = (x2-x1)/2
, dy = (y2-y1)/2
然后从(cx-dy, cy+dx)
画一条线到 (cx+dy, cy-dx)
。
这是可行的,因为 (cx, cy)
是您想要的线段的中点,然后您只需从该中点获取向量到 (x2,y2)
并将其旋转正负 90 度以找到要绘制的线段的端点。
关于javascript - 在谷歌地图中画一条垂直于两点的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5839883/