以米为单位计算两个位置之间的距离 [英] Calculate distance between two locations in metre
问题描述
我有两个地点,我想用米计算距离。
private void getDistancBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2)我写了一些代码,但它并不完美。
{
位置loc1 =新位置();
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);
位置loc2 =新位置();
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);
int R = 6371; // km
double dLat = deg2rad(lat2-lat1);
double dLon = deg2rad(lon2-lon1);
double a =
Math.sin(dLat / 2)* Math.sin(dLat / 2)+
Math.cos(deg2rad(lat1))* Math.cos(deg2rad(lat2 ))*
Math.sin(dLon / 2)* Math.sin(dLon / 2)
;
double c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
double distanceInMeters = R * c;
Log.e(distanceInMeters,distanceInMeters / 10000 +mm);
}
public double deg2rad(double deg){
return deg *(Math.PI / 180);
}
如何以米为单位计算距离?我的目标是,如果米> 200做一些事情。如何解决我的问题?
无需为此重新发明轮子。您可以使用 Location.distanceBetween()
方法。
From 文档:
计算两个地点之间的近似距离(单位:米)
blockquote>
下面是一个简单的例子:
private float getDistancBetweenTwoPoints (double lat1,double lon1,double lat2,double lon2){
float [] distance = new float [2];
Location.distanceBetween(lat1,lon1,
lat2,lon2,distance);
返回距离[0];
$ b $ p
$ b如果结果数组不仅包含索引零,包含一个方位(初始和最终)。
方位是指定罗盘方向的数字。
从 http:// www。 geomidpoint.com/destination/help.html :
方位角(或方位角)是从
的起始点,并且必须在0到360的范围内。0表示
北,90表示东,180表示南,270表示西。
I have two locations and I want to calculate distance in meter. I wrote some code but it's not working perfectly.
private void getDistancBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2) { Location loc1 = new Location(""); loc1.setLatitude(lat1); loc1.setLongitude(lon1); Location loc2 = new Location(""); loc2.setLatitude(lat2); loc2.setLongitude(lon2); int R = 6371; // km double dLat = deg2rad(lat2-lat1); double dLon = deg2rad(lon2-lon1); double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2) ; double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); double distanceInMeters = R * c; Log.e("distanceInMeters",distanceInMeters/10000+"mm"); } public double deg2rad(double deg) { return deg * (Math.PI/180); }
How can I calculate distance in meter? My goal is, if meter > 200 do something. How can i solve my problem?
解决方案There is no need to re-invent the wheel for this.
You can just use the
Location.distanceBetween()
method.From the documentation:
Computes the approximate distance in meters between two locations
Here is a simple example:
private float getDistancBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2) { float[] distance = new float[2]; Location.distanceBetween( lat1, lon1, lat2, lon2, distance); return distance[0]; }
If the result array has more than just index zero populated, the other indices each contain a bearing (initial and final).
A bearing is a number that specifies a compass direction.
From http://www.geomidpoint.com/destination/help.html :
The bearing (or azimuth) is the compass direction to travel from the starting point, and must be within the range 0 to 360. 0 represents north, 90 is east, 180 is south and 270 is west.
这篇关于以米为单位计算两个位置之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!