2011-11-08 296 views
49

请一些线索这种情况计算距离

现在我有有附近地点的纬度和经度×2个阵列,也有用户位置latiude和longiude现在我想计算之间的距离用户位置和附近的地方,并希望在列表视图中显示它们。

我知道,有计算距离

public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results); 

的方法就是现在的问题是如何通过这两个阵列,具有在此方法中附近的纬度和longitue并获得距离的阵列。

+0

退房此链接: http://stackoverflow.com/questions/5739734 /如何计算两点之间的距离在android应用程序/ 5739789#5739789 何这将帮助你。 –

+0

感谢您的回复,但我想用上述方法计算距离。 –

+0

请澄清,从技术上讲,您只给了我们一个方法签名,这是没有足够的信息知道方法文档可以找到的地方。尝试添加有关包含类的信息 – Marmoy

回答

140

http://developer.android.com/reference/android/location/Location.html

查找到distanceTo或distanceBetween。您可以从纬度和经度创建Location对象:

Location locationA = new Location("point A"); 

locationA.setLatitude(latA); 
locationA.setLongitude(lngA); 

Location locationB = new Location("point B"); 

locationB.setLatitude(latB); 
locationB.setLongitude(lngB); 

float distance = locationA.distanceTo(locationB); 

private double meterDistanceBetweenPoints(float lat_a, float lng_a, float lat_b, float lng_b) { 
    float pk = (float) (180.f/Math.PI); 

    float a1 = lat_a/pk; 
    float a2 = lng_a/pk; 
    float b1 = lat_b/pk; 
    float b2 = lng_b/pk; 

    double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2); 
    double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2); 
    double t3 = Math.sin(a1) * Math.sin(b1); 
    double tt = Math.acos(t1 + t2 + t3); 

    return 6366000 * tt; 
} 
+0

这是否通过公路给两地之间的距离? –

+1

不,它返回位移(两点之间的最短距离) –

+1

'float pk =(float)(180/3.14169);' - 小拼写错误,这不是Pi ...它应该是3.141 ** 5 ** 9或使用'Math.PI'(也''Math'类而不赞成'FloatMath')。 57 upvotes和没有人注意到? :D – snachmsm

3

只有一个用户位置,所以你可以迭代附近的地方列表可以调用distanceTo()函数来获得距离,如果你喜欢,你可以存储在一个数组中。

据我所知,distanceBetween()是为了很远的地方,它的输出是一个WGS84椭球体。

+0

可以请你建议哪一个可以提供精确的距离? –

0

distanceTo会给你两个给定位置EJ target.distanceTo(目标)之间米的距离。

distanceBetween也给你的距离,但它会存储在一个float(结果[0])数组中的距离。该文档说,如果结果长度大于或等于2,则初始方位将存储在结果[1]中。如果结果有长度为3或更大,最终的轴承被存储在结果[2]

希望这有助于

我用distanceTo获得从A点到B的距离,我认为是这样的去。

10

试试此代码。这里我们有两个经度和纬度值,而selected_location.distanceTo(near_locations)函数以米为单位返回这些地点之间的距离。

Location selected_location=new Location("locationA"); 
      selected_location.setLatitude(17.372102); 
      selected_location.setLongitude(78.484196); 
Location near_locations=new Location("locationB"); 
      near_locations.setLatitude(17.375775); 
      near_locations.setLongitude(78.469218); 
double distance=selected_location.distanceTo(near_locations); 

这里的 “距离” 是locationA & locationB之间的距离(米)

+4

伟大的提及度量.. !!! –

3
private static Double _MilesToKilometers = 1.609344; 
    private static Double _MilesToNautical = 0.8684; 


    /// <summary> 
    /// Calculates the distance between two points of latitude and longitude. 
    /// Great Link - http://www.movable-type.co.uk/scripts/latlong.html 
    /// </summary> 
    /// <param name="coordinate1">First coordinate.</param> 
    /// <param name="coordinate2">Second coordinate.</param> 
    /// <param name="unitsOfLength">Sets the return value unit of length.</param> 
    public static Double Distance(Coordinate coordinate1, Coordinate coordinate2, UnitsOfLength unitsOfLength) 
    { 

     double theta = coordinate1.getLongitude() - coordinate2.getLongitude(); 
     double distance = Math.sin(ToRadian(coordinate1.getLatitude())) * Math.sin(ToRadian(coordinate2.getLatitude())) + 
         Math.cos(ToRadian(coordinate1.getLatitude())) * Math.cos(ToRadian(coordinate2.getLatitude())) * 
         Math.cos(ToRadian(theta)); 

     distance = Math.acos(distance); 
     distance = ToDegree(distance); 
     distance = distance * 60 * 1.1515; 

     if (unitsOfLength == UnitsOfLength.Kilometer) 
      distance = distance * _MilesToKilometers; 
     else if (unitsOfLength == UnitsOfLength.NauticalMiles) 
      distance = distance * _MilesToNautical; 

     return (distance); 

    } 
0
public double distance(Double latitude, Double longitude, double e, double f) { 
     double d2r = Math.PI/180; 

     double dlong = (longitude - f) * d2r; 
     double dlat = (latitude - e) * d2r; 
     double a = Math.pow(Math.sin(dlat/2.0), 2) + Math.cos(e * d2r) 
       * Math.cos(latitude * d2r) * Math.pow(Math.sin(dlong/2.0), 2) 
     double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
     double d = 6367 * c; 
       return d; 

    }