0

我有一个问题要从我的 2 个地理点获得正确的方法来获取 distanceTo。如何让它工作?

来自 gps 类:

double latitude; // latitude
String mlat;

    latitude = location.getLatitude();
    mlat = String.valueOf(latitude);

从列表视图:

                        // Get distance
                        String mReportA;
                        double mLocB;
                        int distance;
                        mReportA = e.getString("lat");
                        mReportA = e.getString("lon");
                        mLocB = gps.latitude;
                        mLocB = gps.longitude;
                        distance = mReportA.distanceTo(mLocB);

Eclipse 中的错误:对于我从 jsondistanceTo(double)获得的 String 类型,该方法未定义e.getString("lat");

4

3 回答 3

3

您可以将纬度和经度值加倍,然后只计算距离。所以不要将双精度值更改为字符串。

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

locationA.setLatitude(orginlat);
locationA.setLongitude(orginlong);

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

locationB.setLatitude(tolat);
locationB.setLongitude(tolong);

Float distance = locationA.distanceTo(locationB);

字符串转换为double

public static double parseDoubleSafely(String str) {
    double result = 0;
    try {
        result = Double.parseDouble(str);
    } catch (NullPointerException npe) {
        //sysout found null
        return 0; 
    } catch (NumberFormatException nfe) {
        //sysout NFE 
        return 0; 
    }
    return result;
}
于 2013-02-14T10:39:49.130 回答
1
// Here is the code to find out the distance between two locations 

float distance;
Location locationA = new Location("A");  

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

Location locationB = new Location("B");  

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

distance = locationA.distanceTo(locationB);
于 2013-02-14T10:38:45.480 回答
1

要计算两个地理点之间的距离,您可以按照以下示例进行操作。

double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();

double endLatitude = lat;
double endLongitude = lng;

float[] results = new float[3];
Location.distanceBetween(currentLatitude, currentLongitude,endLatitude, endLongitude, results);

BigDecimal bd = new BigDecimal(results[0]);// results in meters
BigDecimal rounded = bd.setScale(2, RoundingMode.HALF_UP);
double values = rounded.doubleValue();

编辑

if (values > 1000) {
 values = (Double) (values * 0.001f);// convert meters to Kilometers
 bd = new BigDecimal(values);
 rounded = bd.setScale(2, RoundingMode.HALF_UP);
 values = rounded.doubleValue();
}     
于 2013-02-14T10:44:07.843 回答