Nice programing

두 지리적 위치 사이의 거리 계산

nicepro 2020. 11. 22. 20:33
반응형

두 지리적 위치 사이의 거리 계산


이 상황에 대해 좀 밝혀주세요

지금은 근처 장소의 위도와 경도를 가진 두 개의 배열이 있으며 사용자 위치 latiude 및 longiude가 있습니다. 이제 사용자 위치와 인근 장소 사이의 거리를 계산하고 목록보기에 표시하고 싶습니다.

거리를 계산하는 방법이 있다는 것을 알고 있습니다.

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

이제 문제는이 방법에서 위도와 경도가 가까운이 두 배열을 전달하고 거리 배열을 얻는 방법입니다.


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

이 코드를 사용해보십시오. 여기에 두 개의 경도 및 위도 값이 있으며 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 사이의 거리 (미터 단위)입니다.


사용자 위치는 하나뿐이므로 주변 장소 목록을 반복 distanceTo()하여 거리를 구할 수있는 함수를 호출 할 수 있으며, 원하는 경우 배열에 저장할 수 있습니다.

내가 이해하는 바에 따르면 distanceBetween()먼 곳에서 출력은 WGS84 타원체입니다.


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

    }

distanceTo는 주어진 두 위치 ej target.distanceTo (destination) 사이의 거리를 미터 단위로 제공합니다.

distanceBetween give you the distance also but it will store the distance in a array of float( results[0]). the doc says If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2]

hope that this helps

i've used distanceTo to get the distance from point A to B i think that is the way to go.


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;

    }

참고URL : https://stackoverflow.com/questions/8049612/calculating-distance-between-two-geographic-locations

반응형