Equivelant of .NET GeoCoordinate.GetDistanceTo in

2019-06-24 15:16发布

问题:

What is the equivelant of the System.Device.Location.GeoCoordinate.GetDistanceTo method in Metro style Windows 8 Applications.

Metro applications have the Geocoordinate class (With a lower case 'C') but no GetDistanceTo method.

Secondly, the Metro Geocoordinate class has no constructor. How can I create an instance of it.

回答1:

The Metro Geocoordinate has no public constructor for some reason. The best way to implement this in my opinion is to use Reflector and copy the implementation of System.Device.Location.GeoCoordinate which will also give you GetDistanceTo.

Hopefully this will be re-added to the API at a later time.



回答2:

I decompiled the .NET 4.5 GetDistanceTo method as per BlackLight. Copying here to save people some effort.

public double GetDistanceTo(GeoCoordinate other)
{
    if (double.IsNaN(this.Latitude) || double.IsNaN(this.Longitude) || double.IsNaN(other.Latitude) || double.IsNaN(other.Longitude))
    {
        throw new ArgumentException(SR.GetString("Argument_LatitudeOrLongitudeIsNotANumber"));
    }
    else
    {
        double latitude = this.Latitude * 0.0174532925199433;
        double longitude = this.Longitude * 0.0174532925199433;
        double num = other.Latitude * 0.0174532925199433;
        double longitude1 = other.Longitude * 0.0174532925199433;
        double num1 = longitude1 - longitude;
        double num2 = num - latitude;
        double num3 = Math.Pow(Math.Sin(num2 / 2), 2) + Math.Cos(latitude) * Math.Cos(num) * Math.Pow(Math.Sin(num1 / 2), 2);
        double num4 = 2 * Math.Atan2(Math.Sqrt(num3), Math.Sqrt(1 - num3));
        double num5 = 6376500 * num4;
        return num5;
    }
}


回答3:

I wrote a small libary to work with coordinates a year ago or something that can help you calculating distances between coordinates. It's available at CodePlex: http://beavergeodesy.codeplex.com/

Calculating distances is then done as easy as this.

// Create a pair of coordinates
GeodeticCoordinate coordinate1 = new GeodeticCoordinate() { Latitude = 63.83451d, Longitude = 20.24655d };
GeodeticCoordinate coordinate2 = new GeodeticCoordinate() { Latitude = 63.85763d, Longitude = 20.33569d };
// Calculate the distance between the coordinates using the haversine formula
double distance = DistanceCalculator.Haversine(coordinate1, coordinate2);