如何计算Android应用程序中两点之间的距离
在我的应用程序中,我试图计算一个人从一个地方到另一个地方的旅行距离。为此,我使用半正矢公式,
R = earth’s radius (mean radius = 6,371km)
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(√a, √(1−a))
d = R.c
获取起始地点和到达地点的纬度和经度,我正在计算以公里为单位的距离。但其他人表示,这种距离计算仅在乘坐飞机出行时有效,如果用户乘坐公路出行,则距离计算会有所不同。
如果是这样,我在道路上行驶时如何获得正确的距离。
请帮助我朋友
in my app i am trying to calculate the distance a person traveling from one place to the other. For that i am using the Haversine formula,
R = earth’s radius (mean radius = 6,371km)
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(√a, √(1−a))
d = R.c
getting the latitude and longitude of starting place and reaching place i am calculating the distance in kms. But others say that this distance calculation works only if travelled by airways and get varies if user travels by roadways.
If it is so how can i get a correct distance while traveling through road ways.
please help me friends
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
此方法在标准 API 中(Location.distanceBetween 方法))
http://developer.android.com/reference/android/location/Location.html
This method is in standard API (Location.distanceBetween method))
http://developer.android.com/reference/android/location/Location.html
使用以下公式计算两个经纬度之间的距离:
将纬度、经度与所需距离的函数和单位(“K”表示公里,“M”表示英里)传递。
希望这会对您有所帮助。
Use the following formula to find the distance between two lat-longs:
Pass lat,longs with the function and unit in which you want distance ("K" for kilometer and "M" for Miles).
Hope this will help you.
Location.distanceBetween()
将为两个纬度经度坐标点执行此操作。Location.distanceBetween()
will do this for two Latitude Longitude coordinate points.当您通过公路旅行(甚至也通过航空旅行)时,地球的方位角就会发挥作用,因为地球并不平坦。但这个半正矢公式应该已经可以解决这个问题了。其次,人们可能会说,当您通过公路旅行时,您不会直接在两点之间行驶。你要转很多圈。为了“精确”地告诉两点之间的距离,您可能还需要考虑这些转弯。
When you travel by road (Even via air-travel too), bearing of the Earth comes into play since the Earth is not flat. but this Haversine formula should already take care of it. Secondly, what people might have been saying, is that, when you travel via road, you don't go just straight between two points. you take quite a number of turns. And to "precisely" tell the distance between two points, you might want to take those turns into consideration too.
我在以下链接中找到了示例代码来计算通过道路的距离。
http://code. google.com/p/krvarma-android-samples/source/browse/#svn/trunk/GPSSample%253Fstate%253Dclose
i found the sample code in the following link to calculate the distance going through roads.
http://code.google.com/p/krvarma-android-samples/source/browse/#svn/trunk/GPSSample%253Fstate%253Dclosed
它的距离似乎更大,因为火车和卡车不是沿直线移动,但飞机却沿直线移动。
您需要路线图来计算距离
Its seems distance trough radways bigger, because trains and trucks moves not along straight line but airplances do.
You need road map to occurate compute distance