두 지리적 지점 사이의 거리를 가져옵니다
사용자가있는 가장 가까운 장소를 확인하는 앱을 만들고 싶습니다. 사용자의 위치를 쉽게 얻을 수 있으며 위도와 경도가있는 장소 목록이 이미 있습니다.
현재 사용자 위치와 가장 가까운 목록의 위치를 아는 가장 좋은 방법은 무엇입니까?
Google API에서 아무것도 찾을 수 없습니다.
Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);
Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);
float distanceInMeters = loc1.distanceTo(loc2);
http://developer.android.com/reference/android/location/Location.html
distanceTo 또는 distanceBetween을 살펴보십시오. 위도와 경도에서 Location 객체를 만들 수 있습니다.
Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);
대략적인 솔루션 (등방 형 투영법을 기반으로 함)이 훨씬 빠릅니다 (1 개의 삼각과 1 제곱근 만 필요).
이 근사치는 점이 너무 멀지 않은 경우에 관련됩니다. 그것은 것입니다 항상 이상 견적 실제 하버 사인 거리에 비교했다. 예를 들어 두 점 사이의 델타 위도 또는 경도가 소수점 이하 4도를 초과하지 않으면 실제 거리에 0.05382 % 를 초과하지 않습니다 .
표준 공식 (Haversine)은 정확한 것입니다 (즉, 지구상의 모든 경도 / 위도에서 작동 합니다). 7 삼각법과 2 제곱근이 필요하기 때문에 훨씬 느립니다 . 두 점이 너무 멀리 떨어져 있지 않고 절대 정밀도가 가장 중요하지 않은 경우이 근사 버전 (사방형)을 사용할 수 있습니다. 삼각법과 삼각근 하나만 사용하기 때문에 훨씬 빠릅니다.
// Approximate Equirectangular -- works if (lat1,lon1) ~ (lat2,lon2)
int R = 6371; // km
double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
double y = (lat2 - lat1);
double distance = Math.sqrt(x * x + y * y) * R;
다음 중 하나를 통해 이를 더욱 최적화 할 수 있습니다 .
- 단순히 거리를 다른 거리와 비교하는 경우 제곱근을 제거하십시오 (이 경우 두 제곱 거리를 비교하십시오).
- 한 마스터 포인트에서 다른 마스터 포인트까지의 거리를 계산하는 경우 코사인을 분해 합니다 (이 경우 마스터 포인트를 중심으로 등 직사각 투영을 수행하므로 모든 비교에 대해 코사인을 한 번 계산할 수 있음).
자세한 내용은 다음을 참조 하십시오 : http://www.movable-type.co.uk/scripts/latlong.html
http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe 에서 여러 언어로 된 Haversine 수식의 멋진 참조 구현이 있습니다 .
몇 가지 방법을 사용할 수 있지만 어떤 방법이 가장 좋은지 결정하려면 먼저 사용자의 고도와 다른 지점의 고도를 알고 있는지 알아야합니까?
당신이 따르는 정확도의 수준에 따라, 당신은 Haversine 또는 Vincenty 공식을 볼 수 있습니다 ...
이 페이지들은 공식을 자세하게 설명하고, 수학적으로 덜 기울어 진 것을 위해 그것들을 스크립트로 구현하는 방법에 대한 설명을 제공합니다!
Haversine Formula : http://www.movable-type.co.uk/scripts/latlong.html
빈센트 포뮬러 : http://www.movable-type.co.uk/scripts/latlong-vincenty.html
공식의 의미에 문제가있는 경우 의견을 말하면 최선을 다해 답변합니다. :)
LatLng 사이의 거리를 얻는 방법에는 두 가지가 있습니다.
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
두번째
public float distanceTo (Location dest)
praveen의 답변으로.
private float getDistance(double lat1, double lon1, double lat2, double lon2) {
float[] distance = new float[2];
Location.distanceBetween(lat1, lon1, lat2, lon2, distance);
return distance[0];
}
Just use the following method, pass it lat and long and get distance in meter:
private static double distance_in_meter(final double lat1, final double lon1, final double lat2, final double lon2) {
double R = 6371000f; // Radius of the earth in m
double dLat = (lat1 - lat2) * Math.PI / 180f;
double dLon = (lon1 - lon2) * Math.PI / 180f;
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(latlong1.latitude * Math.PI / 180f) * Math.cos(latlong2.latitude * Math.PI / 180f) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2f * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c;
return d;
}
you can get distance and time using google Map API Google Map API
just pass downloaded JSON to this method you will get real time Distance and Time between two latlong's
void parseJSONForDurationAndKMS(String json) throws JSONException {
Log.d(TAG, "called parseJSONForDurationAndKMS");
JSONObject jsonObject = new JSONObject(json);
String distance;
String duration;
distance = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getString("text");
duration = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("duration").getString("text");
Log.d(TAG, "distance : " + distance);
Log.d(TAG, "duration : " + duration);
distanceBWLats.setText("Distance : " + distance + "\n" + "Duration : " + duration);
}
참고URL : https://stackoverflow.com/questions/2741403/get-the-distance-between-two-geo-points
'program story' 카테고리의 다른 글
DIV를 가로 및 세로 가운데에 (0) | 2020.08.06 |
---|---|
java.lang.IllegalArgumentException : AppCompat가 현재 테마 기능을 지원하지 않습니다 (0) | 2020.08.06 |
MKMapView의 확대 / 축소 수준 설정 (0) | 2020.08.06 |
NA의 특정 열을 포함하는 행 생략 (0) | 2020.08.06 |
Angularjs : 오류 : [ng : areq] 인수 'HomeController'는 (는) 함수가 아니며 정의되지 않았습니다. (0) | 2020.08.06 |