0
랜드 마크의 위도와 경도의 배열이 있습니다. 사용자 위치에서 반경 10km의 랜드 마크를 얻으려면 랜드 마크와 사용자 위치 간 거리를 어떻게 계산합니까?사용자 위치 근처의 위치 얻기
랜드 마크의 위도와 경도의 배열이 있습니다. 사용자 위치에서 반경 10km의 랜드 마크를 얻으려면 랜드 마크와 사용자 위치 간 거리를 어떻게 계산합니까?사용자 위치 근처의 위치 얻기
private static double distanceInKm(double lat1, double lon1, double lat2, double lon2) {
int R = 6371; // km
double x = (lon2 - lon1) * Math.cos((lat1 + lat2)/2);
double y = (lat2 - lat1);
return (Math.sqrt(x * x + y * y) * R)/1000;
}
또는
Location location1 = new Location("");
location1.setLatitude(latitude1);
location1.setLongitude(longitude1);
Location location2 = new Location("");
location2.setLatitude(latitude2);
location2.setLongitude(longitude2);
float distanceInKm = (location1.distanceTo(location2))/1000;
현재 위치가 LatLong 인 경우 다음과 같은 거리를 계산할 수 있습니다. LatLongs는 아래 코드를 사용합니다.
public float distance (float lat_a, float lng_a, float lat_b, float lng_b)
{
double earthRadius = 3958.75;
double latDiff = Math.toRadians(lat_b-lat_a);
double lngDiff = Math.toRadians(lng_b-lng_a);
double a = Math.sin(latDiff /2) * Math.sin(latDiff /2) +
Math.cos(Math.toRadians(lat_a)) * Math.cos(Math.toRadians(lat_b)) *
Math.sin(lngDiff /2) * Math.sin(lngDiff /2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double distance = earthRadius * c;
int meterConversion = 1609;
return new Float(distance * meterConversion).floatValue();
}
감사는 :) 일 – pavlos