如何计算一个点与另一个点相距一定距离的纬度?
为了在地图上画一个圆,我有一个中心GLatLng(A)和一个半径(r)以米为单位。
这是一个图:
-----------
--/ \--
-/ \-
/ \
/ \
/ r \
| *-------------*
\ A / B
\ /
\ /
-\ /-
--\ /--
-----------
如何计算位置B的GLatLng?假设r平行于赤道。
使用GLatLng.distanceFrom()方法获得给定A和B时的半径是微不足道的-但反之则不然。似乎我需要做一些更重的数学运算。
-
我们将需要一种方法,该方法会在给定方位角和距源点的距离时返回目标点。幸运的是,克里斯·韦尼斯(ChrisVeness)在“计算距离,纬度点之间的距离,方位及更多”方面有一个很好的JavaScript实现。
以下内容已适应与
google.maps.LatLng
该类一起使用:Number.prototype.toRad = function() { return this * Math.PI / 180; } Number.prototype.toDeg = function() { return this * 180 / Math.PI; } google.maps.LatLng.prototype.destinationPoint = function(brng, dist) { dist = dist / 6371; brng = brng.toRad(); var lat1 = this.lat().toRad(), lon1 = this.lng().toRad(); var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1) * Math.sin(dist) * Math.cos(brng)); var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1), Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2)); if (isNaN(lat2) || isNaN(lon2)) return null; return new google.maps.LatLng(lat2.toDeg(), lon2.toDeg()); }
您只需按以下方式使用它:
var pointA = new google.maps.LatLng(25.48, -71.26); var radiusInKm = 10; var pointB = pointA.destinationPoint(90, radiusInKm);
这是使用Google Maps APIv3的完整示例:
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <title>Google Maps Geometry</title> <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> </head> <body> <div id="map" style="width: 400px; height: 300px"></div> <script type="text/javascript"> Number.prototype.toRad = function() { return this * Math.PI / 180; } Number.prototype.toDeg = function() { return this * 180 / Math.PI; } google.maps.LatLng.prototype.destinationPoint = function(brng, dist) { dist = dist / 6371; brng = brng.toRad(); var lat1 = this.lat().toRad(), lon1 = this.lng().toRad(); var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1) * Math.sin(dist) * Math.cos(brng)); var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1), Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2)); if (isNaN(lat2) || isNaN(lon2)) return null; return new google.maps.LatLng(lat2.toDeg(), lon2.toDeg()); } var pointA = new google.maps.LatLng(40.70, -74.00); // Circle center var radius = 10; // 10km var mapOpt = { mapTypeId: google.maps.MapTypeId.TERRAIN, center: pointA, zoom: 10 }; var map = new google.maps.Map(document.getElementById("map"), mapOpt); // Draw the circle new google.maps.Circle({ center: pointA, radius: radius * 1000, // Convert to meters fillColor: '#FF0000', fillOpacity: 0.2, map: map }); // Show marker at circle center new google.maps.Marker({ position: pointA, map: map }); // Show marker at destination point new google.maps.Marker({ position: pointA.destinationPoint(90, radius), map: map }); </script> </body> </html>
更新:
pointA
在北极附近绘制,半径为1,000km:var pointA = new google.maps.LatLng(85, 0); // Close to north pole var radius = 1000; // 1000km
-
围绕另一个点旋转一个点(2D)
2021-01-31 关注 0 浏览47 1答案
-
活塞由一个止点移动到另一个止点的过程称为冲程。
2022-05-08 关注 0 浏览14 1答案
-
如何通过另一个计算列使用一个计算列
2021-03-11 关注 0 浏览78 1答案
-
如何将一个MultiIndex DataFrame与另一个的MultiIndex切片
2021-01-29 关注 0 浏览79 1答案
-
如何从一个QListWidget拖放到另一个
2021-01-29 关注 0 浏览93 1答案
-
如何从一个分支到另一个分支
2022-07-28 关注 0 浏览17 1答案
-
如何通过另一个线程停止一个线程?
2021-01-30 关注 0 浏览247 1答案
-
如何从另一个类中运行的另一个线程更新UI
2021-02-02 关注 0 浏览111 1答案
-
如何在另一个页面上的另一个类中访问mysqli连接?
2021-02-02 关注 0 浏览79 1答案
-
如何将属性从一个bean复制到另一个类中的另一个bean?[重复]
2021-01-30 关注 0 浏览97 1答案