【问题标题】:Calculate a point on a map given one point and a distance给定一个点和距离,计算地图上的一个点
【发布时间】:2018-11-15 19:46:19
【问题描述】:

我正在使用谷歌地图进行一个角度项目。我有一个圆的中心点和以米为单位的半径长度。我试图找到圆周上任何点的坐标 - 我只需要一个。鉴于我所拥有的,我该如何计算?

示例数据:

Center = {
   Latitude : 53.388922117675236
   Longitude : -6.280994415283203
}
Radius = 527

【问题讨论】:

  • @NickParsons 对不起,我应该澄清一下,我只需要一点
  • 好的,圆周上的一个点应该是let x = Center.Latitude+Radiuslet y = Center.Longitude如果我理解你的问题:P
  • 这意味着 x 坐标是 53.388 + 527 = 605.392。半径以米为单位,所以我无法将其添加到坐标中
  • @abyrne85 一个距离和一个点会给你一个圆的无限点。如果你只想要一个点,你应该提供另一个约束。

标签: javascript google-maps math


【解决方案1】:

如果您需要给定距离的任意点,最简单的方法是选择子午方向

NewLong = Long
NewLat = Lat +- Radius / 111111.111  (meters per degree)

或平行方向:

NewLat = Lat
NewLong = Long +- Radius / 111111.111 / Cos(Lat)  

对于随机方向生成随机方位并使用this page中的公式

Formula:
φ2 = asin( sin φ1 ⋅ cos δ + cos φ1 ⋅ sin δ ⋅ cos θ )
λ2 = λ1 + atan2( sin θ ⋅ sin δ ⋅ cos φ1, cos δ − sin φ1 ⋅ sin φ2 )
where   φ is latitude, λ is longitude, 
        θ is the bearing (clockwise from north), 
        δ is the angular distance d/R; 
        d being the distance travelled, R the earth’s radius

JavaScript:(all angles in radians)
var φ2 = Math.asin( Math.sin(φ1)*Math.cos(d/R) +
                    Math.cos(φ1)*Math.sin(d/R)*Math.cos(brng) );
var λ2 = λ1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(φ1),
                         Math.cos(d/R)-Math.sin(φ1)*Math.sin(φ2));

【讨论】:

  • 这当然是正确的数学。但是 1)您没有提供 Javascript 版本,并且 2)您不需要手动计算,因为 API 有一个方法(请参阅我的答案)。
  • @MrUpsidown 我不相信 JS 家伙不能在 JS 中编写给定的公式 ;)
  • Easy ;) 但是当问题提到 JS 时,我尝试提供 JS。显然不是什么大问题!感谢更新!
【解决方案2】:

Maps API 有一个geometry library,可用于计算距离、航向等。

因此,如果您需要在距另一个点的给定距离处找到一个点,无论朝向如何,您都可以使用computeOffset 方法。

以下代码的作用是,它在位置 0,0 (var myLatLng) 添加一个标记,然后在距该点 5000 米的位置添加另一个标记 0(北)。当然,如果需要,您可以更改标题。

function initialize() {

    var myLatLng = new google.maps.LatLng(0,0);
    var mapOptions = {
        zoom: 10,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var point = google.maps.geometry.spherical.computeOffset(myLatLng, 5000, 0);
    
    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
    
    new google.maps.Marker({
        position: myLatLng,
        map: map,
        title: 'Origin marker'
    });
    
    new google.maps.Marker({
        position: point,
        map: map,
        title: 'Offset marker'
    });
}

initialize();
#map-canvas {
  height: 200px;
}
<div id="map-canvas"></div>

<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>

注意 API 的加载方式,包括几何库。

【讨论】:

  • 我已经在使用几何库所以这是完美的。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多