2011-11-02 163 views
1

我正在開發一個Android應用程序,我想添加商店定位器詳細信息和地圖視圖。我查了很多,但我沒有得到任何有意義的東西。我很緊張,非常需要你的幫助。源代碼找到兩個地理點之間的距離

問題是,我想要找到2個座標之間的距離,我有一個Haversine公式,但不知道如何成功運行程序,因爲我是Android的初學者,請幫助做在逐步編碼,即XML代碼也..,請,請,

+0

嘿在這篇文章中你明白了。 http://stackoverflow.com/questions/2741403/get-the-distance-between-two-geo-points – 2011-11-02 09:30:45

回答

4

如上所述,位置類是要走的路。

這裏是我已經使用的代碼:

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

locationA.setLatitude(pointA.getLatitudeE6()/1E6); 
locationA.setLongitude(pointB.getLongitudeE6()/1E6); 

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

locationB.setLatitude(pointB.getLatitudeE6()/1E6); 
locationB.setLongitude(pointB.getLongitudeE6()/1E6); 

double distance = locationA.distanceTo(locationB); 

在這個例子中,兩個點ApointB是GeoPoint的類的實例。

2

從MOTODEV片段(GPS座標之間的距離):

Location originLocation = new Location("gps"); 
Location destinationLocation = new Location("gps"); 
originLocation.setLatitude(originLatitude); 
originLocation.setLongitude(originLongitude); 
destinationLocation.setLatitude(originLatitude); 
destinationLocation.setLongitude(originLongitude); 
float distance = originLocation.distanceTo(destinationLocation); 
0

使用此代碼。你會得到兩個地理點之間的距離。

private String distance(double lati, double longi, double curlati1, 
     double curlongi1) { 

    double theta = longi - curlongi1; 
    double dist = Math.sin(deg2rad(lati)) * Math.sin(deg2rad(curlati1)) 
      + Math.cos(deg2rad(lati)) * Math.cos(deg2rad(curlati1)) 
      * Math.cos(deg2rad(theta)); 
    dist = Math.acos(dist); 
    dist = rad2deg(dist); 
    dist = dist * 60 * 1.1515; 

    dist = dist * 1.609344; 

    result = Double.toString(dist); 
    System.out.println("dist_result :" + result); 
    return (result); 
} 

/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
/* :: This function converts decimal degrees to radians : */ 
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
private double deg2rad(double deg) { 
    return (deg * Math.PI/180.0); 
} 

/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
/* :: This function converts radians to decimal degrees : */ 
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
private double rad2deg(double rad) { 
    return (rad * 180.0/Math.PI); 
} 
相關問題