Calculate distance between two points in bing maps

2020-06-06 07:05发布

问题:

Ihave a bing map, and a two points : Point1,Point2 and i want to calculate the distance between these two points? is that possible? and if i want to put a circle on the two third of the path between point1 and point2 and near point2 ...how can i make it?

回答1:

See Haversine or even better the Vincenty formula how to solve this problem.

The following code uses haversines way to get the distance:

public double GetDistanceBetweenPoints(double lat1, double long1, double lat2, double long2)
    {
        double distance = 0;

        double dLat = (lat2 - lat1) / 180* Math.PI;
        double dLong = (long2 - long1) / 180 * Math.PI;

        double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2)
                    + Math.Cos(lat1 / 180* Math.PI) * Math.Cos(lat2 / 180* Math.PI) 
                    * Math.Sin(dLong/2) * Math.Sin(dLong/2);
        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

        //Calculate radius of earth
        // For this you can assume any of the two points.
        double radiusE = 6378135; // Equatorial radius, in metres
        double radiusP = 6356750; // Polar Radius

        //Numerator part of function
        double nr = Math.Pow(radiusE * radiusP * Math.Cos(lat1 / 180 * Math.PI), 2);
        //Denominator part of the function
        double dr = Math.Pow(radiusE * Math.Cos(lat1 / 180 * Math.PI), 2)
                        + Math.Pow(radiusP * Math.Sin(lat1 / 180 * Math.PI), 2);
        double radius = Math.Sqrt(nr / dr);

        //Calculate distance in meters.
        distance = radius * c;
        return distance; // distance in meters
    }

You can find a good site with infos here.



回答2:

Microsoft has a GeoCoordinate.GetDistanceTo Method, which uses the Haversine formula.

For me other implementation return NaN for distances that are too small. I haven't run into any issues with the built in function yet.



回答3:

You can use a geographic library for (re-)projection and calculation operations if you need more accurate results or want to do some math operations (e.g. transform a circle onto a sperioid/projection). Take a look at DotSpatial or SharpMap and the samples/unittests/sources there... this might help to solve your problem.

Anyway if you know the geodesic distance and bearing you can also calculate where resulting target position (center of your circle) is, e.g. see "Direct Problem" of Vincenty's algorithms. Here are also some useful algorithm implementations for silverlight/.net

You might also consider to post your questions at GIS Stackexchange. They discuss GIS related problems like yours. Take a look at the question for calculating lat long x-miles from point (as you already know the whole distance now) or see the discussion here about distance calculations. This question is related to the problem how to draw a point on a line in a given distance and is nearly the same (cause you need a center and radius).

Another option is to use ArcGIS API for Silverlight which can also display Bing Maps. It is open source and you can learn the things you need there (or just use them, cause they already exists in the SDK). See the Utilities examples tab within the samples.



回答4:

As I already mentioned: Take a look at this page to get more infos regarding your problem. There you'll find a formula, Javascript code and an Excel sample for calculating a destination point by a given distance and bearing from start point (see headlines there).

It shouldn't be difficult to "transform" the code to your c#-world.