问题描述
我有两个坐标,我想绘制一条等长的垂直线。有没有简单的谷歌地图偏移量或干净的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(位置[i] [1],位置[i] [2]),
map:map
});
}
var borderPlanCoordinates = [
new google.maps.LatLng(llat,position.coords.longitude),
new google.maps.LatLng(position。 coords.latitude,llng)
];
borderPath = new google.maps.Polyline({
path:borderPlanCoordinates,
strokeColor:#FF0000,
strokeOpacity:1.0,
strokeWeight:10,
map:map
});
borderPath.setMap(map);
c>(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度,以找到要绘制的线段的端点。
I have two coordinates for which I would like to draw a perpendicular line of equal length. Is there either a simple google maps offset for this or a clean javascript approach by which I might accomplish this? What would that be?
Here is what I have thus far. As you can see, I plot the two points as markers and then attempt to draw a line between them, except I need to get that line perpendicular to the line between the two coordinates.
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);
So you have two points with coordinates (x1,y1)
and (x2, y2)
and you want to draw a line segment whose length is the distance between them, which is the perpendicular bisector of the segment connecting them, and which is bisected by said segment?
Probably the simplest way is to set cx = (x1 + x2)/2
, cy = (y1+y2)/2)
, dx = (x2-x1)/2
, dy = (y2-y1)/2
and then draw a line from (cx-dy, cy+dx)
to (cx+dy, cy-dx)
.
This works because (cx, cy)
is the midpoint of the segment you want, and then you're just taking the vector from that midpoint to (x2,y2)
and rotating it by plus and minus 90 degrees to find the endpoints of the segment you want to draw.
这篇关于在谷歌地图上画一条垂直于两点的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!