【问题标题】:I want to get exact the distance between two latitudes and longitudes我想得到准确的两个纬度和经度之间的距离
【发布时间】:2018-10-11 09:32:17
【问题描述】:

我想得到两个刚刚到来的纬度和经度之间的确切差异。为了实现这一点,我尝试了很多代码。喜欢:

GeodeticCalculator geoCalc = new GeodeticCalculator();
Ellipsoid reference = Ellipsoid.WGS84;
Double dbLat = Double.valueOf(latLong[0]);
Double dbLong = Double.valueOf(latLong[1]);
Toast.makeText(this, "" + dbLat + "," + dbLong, Toast.LENGTH_SHORT).show();
GlobalPosition pointA = new GlobalPosition(Double.valueOf(latLong[0]), Double.valueOf(latLong[1]), 0.0);
GlobalPosition userPos = new GlobalPosition(lat2, long2, 0.0);
distance = geoCalc.calculateGeodeticMeasurement(reference, userPos, pointA).getPointToPointDistance();

还有

public static double getDistance2(double lat1, double lon1, double lat2, double lon2) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1))
            * Math.sin(deg2rad(lat2))
            + Math.cos(deg2rad(lon1))
            * Math.cos(deg2rad(lon2))
            * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    return (dist);
}

private static double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}

private static  double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}

public static Double getDistance1(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 6371000; //meters
    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
            Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                    Math.sin(dLng / 2) * Math.sin(dLng / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    Double dist = (Double) (earthRadius * c);
    return dist;
}

最后,

public static Double getDistance(Double lat1,Double long1,Double lat2,Double long2){
    Location startPoint=new Location("locationA");
    startPoint.setLatitude(lat1);
    startPoint.setLongitude(long1);

    Location endPoint=new Location("locationA");
    endPoint.setLatitude(lat2);
    endPoint.setLongitude(long2);

    double distance=startPoint.distanceTo(endPoint);
    return distance;
}

但是直到现在我都无法获得确切的位置。所以请帮我解决我的问题。

【问题讨论】:

  • 定义exact (1) 线性 (2) 球形
  • 哪个最好。
  • Ofcouse Spherical......因为在Linear中,路线中可能有obsticals。
  • 好的,先生。所以现在帮助我实现这一目标
  • 不清楚你尝试的方法有什么问题,你期望什么结果,你得到了什么?

标签: android


【解决方案1】:

希望下面的代码对你有所帮助:

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);

【讨论】:

    猜你喜欢
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    相关资源
    最近更新 更多