위도/경도 좌표 세트 사이의 거리를 어떻게 결정할 수 있습니까?
-
18-09-2019 - |
문제
나는 LAT/LON 좌표 세트 사이의 거리를 결정할 무언가를 쓰려고 노력하고 있습니다.
내가 찾은 다음 코드를 사용하고 있습니다. 이 지역:
public static double distance (double lat1, double lon1, double lat2, double lon2) {
double lat1 = Convert.ToDouble(latitude);
double lon1 = Convert.ToDouble(longitude);
double lat2 = Convert.ToDouble(destlat);
double lon2 = Convert.ToDouble(destlon);
double theta = toRadians(lon1-lon2);
lat1 = toRadians(lat1);
lon1 = toRadians(lon1);
lat2 = toRadians(lat2);
lon2 = toRadians(lon2);
double dist = sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(theta);
dist = toDegrees(acos(dist)) * 60 * 1.1515 * 1.609344 * 1000;
return dist;
}
내 문제는 컴파일 오류가 발생한다는 것입니다. " 'Toradians'/'cos'/'sin/'todegrees '라는 이름은 현재 상황에 존재하지 않습니다 ..." 내가 뭘 잘못하고 있죠?
해결책
다음 C# 클래스를 사용하고 싶을 수도 있습니다.
public static class GeoCodeCalc
{
public const double EarthRadiusInMiles = 3956.0;
public const double EarthRadiusInKilometers = 6367.0;
public static double ToRadian(double val) { return val * (Math.PI / 180); }
public static double DiffRadian(double val1, double val2) { return ToRadian(val2) - ToRadian(val1); }
public static double CalcDistance(double lat1, double lng1, double lat2, double lng2)
{
return CalcDistance(lat1, lng1, lat2, lng2, GeoCodeCalcMeasurement.Miles);
}
public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, GeoCodeCalcMeasurement m)
{
double radius = GeoCodeCalc.EarthRadiusInMiles;
if (m == GeoCodeCalcMeasurement.Kilometers) { radius = GeoCodeCalc.EarthRadiusInKilometers; }
return radius * 2 * Math.Asin( Math.Min(1, Math.Sqrt( ( Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
}
}
public enum GeoCodeCalcMeasurement : int
{
Miles = 0,
Kilometers = 1
}
용법:
// Calculate Distance in Miles
GeoCodeCalc.CalcDistance(47.8131545175277, -122.783203125, 42.0982224111897, -87.890625);
// Calculate Distance in Kilometers
GeoCodeCalc.CalcDistance(47.8131545175277, -122.783203125, 42.0982224111897, -87.890625, GeoCodeCalcMeasurement.Kilometers);
다른 팁
당신은 쓸 수 있습니다 toRadians
다음과 같은 기능 :
double ToRadians(double degrees) { return degrees * Math.PI / 180; }
당신은 쓸 수 있습니다 toDegrees
다음과 같은 기능 :
double ToDegrees(double radians) { return radians * 180 / Math.PI; }
당신은 교체해야합니다 sin
그리고 cos
~와 함께 Math.Sin
그리고 Math.Cos
.
이것은 C#처럼 보입니다.
먼저 정의해야합니다 toRadians
그리고 toDegrees
:
double toRadians(double degrees) {
double sign = Math.Sign(degrees);
while(Math.Abs(degrees) > 360) {
degrees -= sign * 360;
}
return Math.PI * degrees / 180;
}
double toDegrees(double radians) {
double sign = Math.Sign(radians);
while(Math.Abs(radians) > 2 * Math.PI) {
radians -= sign * 2 * Math.PI;
}
return 180 * radians / Math.PI;
}
그런 다음 삼각 함수를 사용하려면 사용해야합니다. Math.Sin
, Math.Cos
, 등.
double dist = Math.Sin(lat1) * Math.Sin(lat2)
+ Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(theta);
그리고
dist = toDegrees(Math.Acos(dist)) * 60 * 1.1515 * 1.609344 * 1000;
코멘트:
public static double distance (double lat1, double lon1, double lat2, double lon2) {
double lat1 = Convert.ToDouble(latitude);
double lon1 = Convert.ToDouble(longitude);
double lat2 = Convert.ToDouble(destlat);
double lon2 = Convert.ToDouble(destlon);
이게 뭔가요? 어디 latitude
, longitude
, destlat
그리고 destlon
한정된? 또한, 당신이 가지고있는 것 같습니다 lat1
, lon1
lat2
그리고 lon2
이 방법의 매개 변수로서 여기에서 동일한 이름으로 현지인을 정의 할 수 없습니다.
double theta = toRadians(lon1-lon2);
lat1 = toRadians(lat1);
lon1 = toRadians(lon1);
lat2 = toRadians(lat2);
lon2 = toRadians(lon2);
이것은 나쁜 스타일입니다. 만약에 lat1
정도의 위도를 나타냅니다. lat1
이와 같이:
double lat1Radians = toRadians(lat1);
따라서 위를 대체하십시오.
double theta = toRadians(lon1-lon2);
double lat1Radians = toRadians(lat1);
double lon1Radians = toRadians(lon1);
double lat2Radians = toRadians(lat2);
double lon2Radians = toRadians(lon2);
마지막으로 :
double dist = sin(lat1) * sin(lat2)
+ cos(lat1) * cos(lat2) * cos(theta);
dist = toDegrees(acos(dist)) * 60 * 1.1515 * 1.609344 * 1000;
이것도 나쁜 스타일입니다. 첫 번째 공식과 두 번째 공식은 둘 다 계산하려는 거리를 나타낼 수 없습니다. 첫 번째 공식의 결과를보다 의미있는 이름으로 변수에 할당해야합니다. 최악의 경우, 최소한 다음을 수행하십시오.
double temp = Math.Sin(lat1) * Math.Sin(lat2)
+ Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(theta);
double dist = toDegrees(Math.Acos(dist)) * 60 * 1.1515 * 1.609344 * 1000;
return dist;
나는이 질문이 정말로 나이가 많다는 것을 알고 있지만, 다른 사람 이이 문제를 발견하는 경우 GeoCoordinate
~에서 System.Device
:
var distanceInMeters = new GeoCoordinate(lat1, lon1)
.GetDistanceTo(new GeoCoordinate(lat2, lon2));
위도와 경도 점 사이의 거리 계산 ...
double lat1 = convert.todouble (위도);
double Long1 = Convert.ToDouble(longitude);
double Lat2 = 30.678;
double Long2 = 45.786;
double circumference = 40000.0; // Earth's circumference at the equator in km
double distance = 0.0;
double latitude1Rad = DegreesToRadians(Lat1);
double latititude2Rad = DegreesToRadians(Lat2);
double longitude1Rad = DegreesToRadians(Long1);
double longitude2Rad = DegreesToRadians(Long2);
double logitudeDiff = Math.Abs(longitude1Rad - longitude2Rad);
if (logitudeDiff > Math.PI)
{
logitudeDiff = 2.0 * Math.PI - logitudeDiff;
}
double angleCalculation =
Math.Acos(
Math.Sin(latititude2Rad) * Math.Sin(latitude1Rad) +
Math.Cos(latititude2Rad) * Math.Cos(latitude1Rad) * Math.Cos(logitudeDiff));
distance = circumference * angleCalculation / (2.0 * Math.PI);
return distance;
이 코드를 조금 조정해야합니다.
Slaks에 따르면, 당신은 자신의 toRadians()
.NET에는 기본 버전이 없기 때문에 메소드.
또한 cos () 및 sin ()로 호출을 변경해야합니다.