找到一定距离的坐标并远离另一个坐标

时间:2016-07-04 20:42:55

标签: javascript google-maps-api-3 geometry

我正在使用 Google地图几何图书馆两个计算两点之间的距离和航向:

...computeDistanceBetween(coord1, coord2) // = 10 m.
...computeHeading(coord1, coord2) // = 25

由此我想找到一个基于另一个已知坐标的点,也就是10米。离开,方向相同(标题):

...computeDistanceBetween(coord3, unknown) // = 10 m.
...computeHeading(coord3, unknown) // = 25

在这种情况下如何计算未知?

1 个答案:

答案 0 :(得分:2)

使用computeOffset。来自the documentation

  

computeOffset(from:LatLng,distance:number,heading:number,radius?:number)

     

返回值:LatLng

     

返回从指定标题中的原点移动距离得到的LatLng(以北方向顺时针方向表示)。

var distance = google.maps.geometry.spherical.computeDistanceBetween(coord1, coord2);
var heading = google.maps.geometry.spherical.computeHeading(coord1, coord2);
var newPt = google.maps.geometry.spherical.computeOffset(coord1, distance/2, heading);

代码段

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var coord1 = new google.maps.LatLng(37.399228, -122.208676); // SouthWest corner
  var coord2 = new google.maps.LatLng(37.484548, -122.075124); // NorthEast corner
  var distance = google.maps.geometry.spherical.computeDistanceBetween(coord1, coord2);
  var heading = google.maps.geometry.spherical.computeHeading(coord1, coord2);
  var newPt = google.maps.geometry.spherical.computeOffset(coord1, distance / 2, heading);
  console.log(newPt.toUrlValue(6) + ", distance=" + distance.toFixed(2) + ", heading=" + heading);
  var marker = new google.maps.Marker({
    position: newPt,
    map: map
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="map_canvas"></div>