使用纬度/经度和公里距离进行简单计算?

发布于 2024-08-01 20:32:00 字数 487 浏览 4 评论 0原文

我可以做一个简单的计算,将 km 转换为一个值,我可以将其添加到纬度或经度浮点数以计算搜索的边界框吗? 它不需要完全准确。

例如:如果给我一个英国伦敦的纬度/经度(51.5001524,-0.1262362),我想计算从该点向东/向西 25 公里的经度是多少,以及从该点向北/向南 25 公里的经度是多少点,我需要做什么才能将 25 公里转换成小数以添加到上面的值中?

我正在寻找一般的经验法则,即:1km == +/- 0.XXX

编辑:

我最初搜索“lat lon”没有返回此结果:

如何计算给定纬度的边界框/lng location?

接受的答案似乎足以满足我的要求。

Is there a simple calculation I can do which will convert km into a value which I can add to a lat or lon float to calculate a bounding box for searches? It doesn't need to be completely accurate.

For instance: if I were given a lat/lon for London, England (51.5001524, -0.1262362) and I wanted calculate what the lat would be 25km east/west from that point, and what the lon would be 25km north/south of that point, what would I need to do to convert the 25km into a decimal to add to the values above?

I'm looking for a general rule-of-thumb, ie: 1km == +/- 0.XXX

Edit:

My original search for "lat lon" didn't return this result:

How to calculate the bounding box for a given lat/lng location?

The accepted answer seems adequate for my requirements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

洋洋洒洒 2024-08-08 20:32:00

近似转换为:

  • 纬度:1 度 = 110.574 公里
  • 经度:1 度 = 111.320*cos(纬度) 公里

这并不完全正确地反映地球极地扁平化的情况 - 为此,您可能需要使用 WGS84 的更复杂的公式参考椭球体(用于 GPS 的模型)。 但对于您的目的来说,该错误可能可以忽略不计。

来源:http://en.wikipedia.org/wiki/Latitude

警告:请注意,经纬度坐标以度数表示,而大多数(所有?)语言中的 cos 函数通常接受弧度,因此 度数需要弧度转换

The approximate conversions are:

  • Latitude: 1 deg = 110.574 km
  • Longitude: 1 deg = 111.320*cos(latitude) km

This doesn't fully correct for the Earth's polar flattening - for that you'd probably want a more complicated formula using the WGS84 reference ellipsoid (the model used for GPS). But the error is probably negligible for your purposes.

Source: http://en.wikipedia.org/wiki/Latitude

Caution: Be aware that latlong coordinates are expressed in degrees, while the cos function in most (all?) languages typically accepts radians, therefore a degree to radians conversion is needed.

标点 2024-08-08 20:32:00

感谢 Jim Lewis 的出色回答,我想通过我在 Swift 中的函数来说明这个解决方案:

func getRandomLocation(forLocation location: CLLocation, withOffsetKM offset: Double) -> CLLocation {
        let latDistance = (Double(arc4random()) / Double(UInt32.max)) * offset * 2.0 - offset
        let longDistanceMax = sqrt(offset * offset - latDistance * latDistance)
        let longDistance = (Double(arc4random()) / Double(UInt32.max)) * longDistanceMax * 2.0 - longDistanceMax
        
        let lat: CLLocationDegrees = location.coordinate.latitude + latDistance / 110.574
        let lng: CLLocationDegrees = location.coordinate.longitude + longDistance / (111.320 * cos(lat * .pi / 180))
        return CLLocation(latitude: lat, longitude: lng)
    }

在这个函数中为了转换距离,我使用以下公式:

latDistance / 110.574
longDistance / (111.320 * cos(lat * .pi / 180))

Thanks Jim Lewis for his great answer and I would like to illustrate this solution by my function in Swift:

func getRandomLocation(forLocation location: CLLocation, withOffsetKM offset: Double) -> CLLocation {
        let latDistance = (Double(arc4random()) / Double(UInt32.max)) * offset * 2.0 - offset
        let longDistanceMax = sqrt(offset * offset - latDistance * latDistance)
        let longDistance = (Double(arc4random()) / Double(UInt32.max)) * longDistanceMax * 2.0 - longDistanceMax
        
        let lat: CLLocationDegrees = location.coordinate.latitude + latDistance / 110.574
        let lng: CLLocationDegrees = location.coordinate.longitude + longDistance / (111.320 * cos(lat * .pi / 180))
        return CLLocation(latitude: lat, longitude: lng)
    }

In this function to convert distance I use following formulas:

latDistance / 110.574
longDistance / (111.320 * cos(lat * .pi / 180))
野の 2024-08-08 20:32:00

如果您使用 Java、Javascript 或 PHP,那么有一个库可以使用一些非常复杂(但仍然很快)的三角函数来精确地执行这些计算:

http://www.jstott.me.uk/jcoord/

If you're using Java, Javascript or PHP, then there's a library that will do these calculations exactly, using some amusingly complicated (but still fast) trigonometry:

http://www.jstott.me.uk/jcoord/

つ低調成傷 2024-08-08 20:32:00

http://www.jstott.me.uk/jcoord/ - 使用此库

            LatLng lld1 = new LatLng(40.718119, -73.995667);
            LatLng lld2 = new LatLng(51.499981, -0.125313);
            Double distance = lld1.distance(lld2);
            Log.d(TAG, "Distance in kilometers " + distance);

http://www.jstott.me.uk/jcoord/ - use this library

            LatLng lld1 = new LatLng(40.718119, -73.995667);
            LatLng lld2 = new LatLng(51.499981, -0.125313);
            Double distance = lld1.distance(lld2);
            Log.d(TAG, "Distance in kilometers " + distance);
唱一曲作罢 2024-08-08 20:32:00

有趣的是我没有看到提及 UTM 坐标。

https://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system

至少如果你想将公里添加到同一区域,它应该很简单(在Python中: https:// pypi.org/project/utm/

utm.from_latlon 和 utm.to_latlon。

Interesting that I didn't see a mention of UTM coordinates.

https://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system.

At least if you want to add km to the same zone, it should be straightforward (in Python : https://pypi.org/project/utm/ )

utm.from_latlon and utm.to_latlon.

左岸枫 2024-08-08 20:32:00

这个更准确(Haversin公式)我们使用地球半径

// distance (in km) between two points specified by latitude/longitude
function calcDistance(lat1, lon1, lat2, lon2) {
  var R = 6371; // km
  var dLat = (lat2-lat1).toRad();
  var dLon = (lon2-lon1).toRad();
  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
          Math.sin(dLon/2) * Math.sin(dLon/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = R * c;
  return d;
}

This is more accurate (Haversin formula) we use the radius of the earth

// distance (in km) between two points specified by latitude/longitude
function calcDistance(lat1, lon1, lat2, lon2) {
  var R = 6371; // km
  var dLat = (lat2-lat1).toRad();
  var dLon = (lon2-lon1).toRad();
  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
          Math.sin(dLon/2) * Math.sin(dLon/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = R * c;
  return d;
}
王权女流氓 2024-08-08 20:32:00

为什么不使用正确制定的​​地理空间查询???

以下是 STContains 地理空间函数的 SQL Server 参考页:

https://learn.microsoft.com/en-us/sql/t-sql/spatial-geography/stcontains-geography-data-type?view=sql -server-ver15

或者如果您不想使用盒子和弧度转换,您可以始终使用距离函数来查找您需要的点:

DECLARE @CurrentLocation geography; 
SET @CurrentLocation  = geography::Point(12.822222, 80.222222, 4326)

SELECT * , Round (GeoLocation.STDistance(@CurrentLocation ),0) AS Distance FROM [Landmark]
WHERE GeoLocation.STDistance(@CurrentLocation )<= 2000 -- 2 Km

几乎所有数据库都应该有类似的功能。

如果您正确实施了地理空间索引,您的搜索将比您正在使用的方法快得多

Why not use properly formulated geospatial queries???

Here is the SQL server reference page on the STContains geospatial function:

https://learn.microsoft.com/en-us/sql/t-sql/spatial-geography/stcontains-geography-data-type?view=sql-server-ver15

or if you do not waant to use box and radian conversion , you cna always use the distance function to find the points that you need:

DECLARE @CurrentLocation geography; 
SET @CurrentLocation  = geography::Point(12.822222, 80.222222, 4326)

SELECT * , Round (GeoLocation.STDistance(@CurrentLocation ),0) AS Distance FROM [Landmark]
WHERE GeoLocation.STDistance(@CurrentLocation )<= 2000 -- 2 Km

There should be similar functionality for almost any database out there.

If you have implemented geospatial indexing correctly your searches would be way faster than the approach you are using

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文