Click here to Skip to main content
15,896,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am developing a Windows 8 App. I have a requirement to find the shortest distance between two locations by road using bing maps. How to do that?

Thanks,
Kalai
Posted

1 solution

Hi Kalai91

try this to calculate distance b/w tow places in window 8 App

C#
private double CalculateDistance(double prevLat, double prevLong, double currLat, double currLong)
       {
           const double degreesToRadians = (Math.PI / 180.0);
           const double earthRadius = 6371; // kilometers

           // convert latitude and longitude values to radians
           var prevRadLat = prevLat * degreesToRadians;
           var prevRadLong = prevLong * degreesToRadians;
           var currRadLat = currLat * degreesToRadians;
           var currRadLong = currLong * degreesToRadians;

           // calculate radian delta between each position.
           var radDeltaLat = currRadLat - prevRadLat;
           var radDeltaLong = currRadLong - prevRadLong;

           // calculate distance
           var expr1 = (Math.Sin(radDeltaLat / 2.0) *
                        Math.Sin(radDeltaLat / 2.0)) +

                       (Math.Cos(prevRadLat) *
                        Math.Cos(currRadLat) *
                        Math.Sin(radDeltaLong / 2.0) *
                        Math.Sin(radDeltaLong / 2.0));

           var expr2 = 2.0 * Math.Atan2(Math.Sqrt(expr1),
                                        Math.Sqrt(1 - expr1));

           var distance = (earthRadius * expr2);
           return distance * 1000;  // return results as meters
       }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900