|
Partner
|
|
|
Berechnung der Entfernung zwischen GPS-Koordinaten
Autor:
Marcell Spies
|
Sprache:
C#
|
Bewertung:
9 (1 Bewertung)
|
Anzahl der Aufrufe:
10900
|
Beschreibung:
Das Snippet ermöglicht die Berechnung zwischen zwei oder mehreren GPS-Koordinaten nach dem Great Circle Distance-Verfahren (http://en.wikipedia.org/wiki/Great-circle_distance).
Als Rückgabe erfolgt ein double-Wert mit der Maßeinheit Kilometer.
Anwendung:
//Luftlinie zwischen //Mannheim -> Ludwigshafen -> Stuttgart -> Berlin
double distance = TrackingHelper.CalculateDistance( new Location() { Latitude = 49.4833270, Longitude = 8.4748935 }, new Location() { Latitude = 49.4822117, Longitude = 8.4378147 }, new Location() { Latitude = 48.7794059, Longitude = 9.1755294 }, new Location() { Latitude = 52.5230417, Longitude = 13.4108447 });
Anmerkung: Zuerst hatte ich es selbst probiert, aber vergessen in Radianten umzurechnen. Dann hatte ich gesucht und zwei bereits existierende Funktionen etwas angepasst und noch zusätzlich erweitert. Hier die beiden ursprünglichen Snippets: http://www.personalmicrocosms.com/Pages/dotnettips.aspx?c=26&t=41#tip http://www.personalmicrocosms.com/Pages/dotnettips.aspx?c=26&t=42#tip
Abgelegt unter: distance, gps, location, longitude, latitude, calculate, math, formula.
|
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
public class Location
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public static class TrackingHelper
{
private static double DegreesToRadians(double degrees)
{
return degrees * Math.PI / 180.0;
}
public static double CalculateDistance(Location location1, Location location2)
{
double circumference = 40000.0; // Erdumfang in km am Äquator
double distance = 0.0;
//Radianten berechnen
double latitude1Rad = DegreesToRadians(location1.Latitude);
double longitude1Rad = DegreesToRadians(location1.Longitude);
double latititude2Rad = DegreesToRadians(location2.Latitude);
double longitude2Rad = DegreesToRadians(location2.Longitude);
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;
}
public static double CalculateDistance(params Location[] locations)
{
double totalDistance = 0.0;
for (int i = 0; i < locations.Length - 1; i++)
{
Location current = locations[i];
Location next = locations[i + 1];
totalDistance += CalculateDistance(current, next);
}
return totalDistance;
}
}
|
|
Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)
|
Roland Climb schrieb am:
04.05.2010 18:21:52
|
Arpad Stoever schrieb am:
01.12.2010 02:46:36
|
|
Diese Snippets könnten für Sie interessant sein:
|
|
|
|
|
|
|
|
|