简体   繁体   English

在谷歌地图中画一条垂直于两点的线

[英]drawing a line in google maps perpendicular to two points

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?是否有一个简单的谷歌地图偏移或一个干净的 javascript 方法,我可以通过它来完成这个? 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.如您所见,我将 plot 这两个点作为标记,然后尝试在它们之间画一条线,但我需要使该线垂直于两个坐标之间的线。

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?因此,您有两个坐标为(x1,y1)(x2, y2)的点,您想绘制一条线段,其长度是它们之间的距离,它是连接它们的线段的垂直平分线,由说段?

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) .可能最简单的方法是设置cx = (x1 + x2)/2 , cy = (y1+y2)/2) , dx = (x2-x1)/2 , dy = (y2-y1)/2然后绘制从(cx-dy, cy+dx)(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.这是有效的,因为(cx, cy)是您想要的线段的中点,然后您只需将该中点的向量带到(x2,y2)并将其旋转正负 90 度以找到端点要绘制的段。

I tried this solution, the middle of the segment is ok BUT it doesn't look perpendicular on google maps, I suspect because of spherical projection (not orthonormal).我尝试了这个解决方案,该段的中间还可以,但它在谷歌地图上看起来并不垂直,我怀疑是因为球面投影(不是正交投影)。

Here is a solution that works:这是一个有效的解决方案:

spherical = google.maps.geometry.spherical;
var F = new google.maps.LatLng(latF, longF); 
var T = new google.maps.LatLng(latT, longT); 
// M is the middle of [FT]
var latM = (latF + latT)/2;
var longM = (longF + longT)/2;
var M =  new google.maps.LatLng(latM, longM);
// Get direction of the segment
var heading = spherical.computeHeading(F, T);
var dist = 200; // distance in meters
// Place point A that is oriented at 90° in a distance of dist from M
var A = spherical.computeOffset(M, dist, heading+90);
var perpendicularCoordinates = [M, A ];
var perpendicular = new google.maps.Polyline({
    path: perpendicularCoordinates,
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2,
    map: map
  });    

Here is the related fiddle: http://jsfiddle.net/8m7vm650/15/这是相关的小提琴: http://jsfiddle.net/8m7vm650/15/

Please note that you need to use an optional google maps library called geometry which is done by adding libraries=geometry to query string when loading maps:http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false请注意,您需要使用一个名为 geometry 的可选谷歌地图库,该库通过在加载地图时向查询字符串添加 library=geometry 来完成:http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM