Calculate distance between two address RRS feed

  • Question

  • I am doing a silverlight bing map application. In the application it have two textboxes and one button. Does anyone know how to make it to C# code when user input two addresses in the two textboxes and it will calculate the distance between two address.

    Monday, July 12, 2010 5:05 AM


All replies

  • get the lat longs for each address and calculate.

    The following is javascript that produces the answer in miles

    		srcLong = srcLong * Math.PI / 180;
    srcLat = srcLat * Math.PI / 180;
    var dstLongR = dstLong * Math.PI / 180;
    var dstLatR = dstLat * Math.PI / 180;
    var earthRadius = 3958.75587; // earth's mean radius in miles (6371km)
    var distance = Math.acos(Math.sin(srcLat)*Math.sin(dstLatR) +
    Math.cos(srcLat)*Math.cos(dstLatR)*Math.cos((dstLongR-srcLong))) * earthRadius;
    distance = Math.round(distance*10)/10;

    if you want to know the driven distance have a look at the bing maps web service api.

    Monday, July 12, 2010 8:38 AM
  • I had already use geocode and route web service however I can only get the route out from one point to another point but not able to calculate the distance. Can you provide me sample C# code so that the distance is able to display out in the textbox when two addresses is enter?

    Monday, July 12, 2010 8:59 AM
  • This is the coding that I had done perhap u may like to point out to me what code I am missing as the distance between two address is not able to calculate out.


    //1. User enter the start & end locations and click calculate botton.

    // This method accepts a geocode query string as well as a ‘waypoint index’, which will refer to the position our result

    // will be stored in results array which indicate either start or end position.

    private void Geocode(string strAddress, int waypointIndex)


    // Create the service variable and set the callback method using the GeocodeCompleted property. GeocodeService.GeocodeServiceClient geocodeService = new GeocodeService.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");

    geocodeService.GeocodeCompleted += new EventHandler(geocodeService_GeocodeCompleted);

    // Set the credentials and the geocode query, which could be an address or location.

    GeocodeService.GeocodeRequest geocodeRequest = new GeocodeService.GeocodeRequest();

    geocodeRequest.Credentials = new Credentials();

    geocodeRequest.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)mMap.CredentialsProvider).ApplicationId; geocodeRequest.Query = strAddress;

    // Make the asynchronous Geocode request, using the ‘waypoint index’ as // the user state to track this request and allow it to be identified when the response is returned. geocodeService.GeocodeAsync(geocodeRequest, waypointIndex);


    // This is the global internal variable where results are stored. These are accessed later to calculate the route.

    internal GeocodeService.GeocodeResult[] geocodeResults;


    // //2. Get the actual coordinates for the locations.

    // This is the Geocode request callback method.

    private void geocodeService_GeocodeCompleted(object sender, GeocodeService.GeocodeCompletedEventArgs e)


    // Retrieve the user state of this response (the ‘waypoint index’) to identify which geocode request

    // it corresponds to.

    int waypointIndex = System.Convert.ToInt32(e.UserState);

    // Retrieve the GeocodeResult for this response and store it in the global variable geocodeResults, using the waypoint index to position it in the array.

    geocodeResults[waypointIndex] = e.Result.Results[0];

    // Look at each element in the global gecodeResults array to figure out if more geocode responses still need to be returned.

    bool doneGeocoding = true;

    foreach (GeocodeService.GeocodeResult gr in geocodeResults)


    if (gr == null) { doneGeocoding = false;



     // If the geocodeResults array is totally filled, then calculate the route.

    if (doneGeocoding) CalculateRoute(geocodeResults); }


    // //3. Pass the coordinates to web service to get the calculated route

    private void route_Click(object sender, RoutedEventArgs e)


    // Initialize the length of the results array. In this sample we have two waypoints.

    geocodeResults = new GeocodeService.GeocodeResult[2];

    // Make the two Geocode requests using the values of the text boxes. Also pass the waypoint indexes of these two values within the route.

    Geocode(txtStart.Text, 0); Geocode(txtEnd.Text, 1);


    // This method makes the initial CalculateRoute asynchronous request using the results of the Geocode Service.

    private void CalculateRoute(GeocodeService.GeocodeResult[] results)


    // Create the service variable and set the callback method using the CalculateRouteCompleted property.

    RouteService.RouteServiceClient routeService = new RouteService.RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += new EventHandler(routeService_CalculateRouteCompleted);

    // Set the credentials.

    RouteService.RouteRequest routeRequest = new RouteService.RouteRequest();

    routeRequest.Credentials = new Credentials(); routeRequest.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)mMap.CredentialsProvider).ApplicationId;

    // Return the route points so the route can be drawn.

    routeRequest.Options = new RouteService.RouteOptions(); routeRequest.Options.RoutePathType = RouteService.RoutePathType.Points;

    // Set the waypoints of the route to be calculated using the Geocode Service results stored in the geocodeResults variable.

    routeRequest.Waypoints = new System.Collections.ObjectModel.ObservableCollection();

    foreach (GeocodeService.GeocodeResult result in results)


    routeRequest.Waypoints.Add(GeocodeResultToWaypoint(result)); }

    // Make the CalculateRoute asnychronous request. routeService.CalculateRouteAsync(routeRequest);



    // //4. Use the returned route to plot on to the map to show the user.

    private RouteService.Waypoint GeocodeResultToWaypoint(GeocodeService.GeocodeResult result)


    RouteService.Waypoint waypoint = new RouteService.Waypoint();

    waypoint.Description = result.DisplayName; waypoint.Location = new Location();

    waypoint.Location.Latitude = result.Locations[0].Latitude;

    waypoint.Location.Longitude = result.Locations[0].Longitude; return waypoint;


    // This is the callback method for the CalculateRoute request.

    private void routeService_CalculateRouteCompleted(object sender, RouteService.CalculateRouteCompletedEventArgs e)


    // If the route calculate was a success and contains a route, then draw the route on the map.

    if ((e.Result.ResponseSummary.StatusCode == RouteService.ResponseStatusCode.Success) & (e.Result.Result.Legs.Count != 0))


    // Set properties of the route line you want to draw.

    Color routeColor = Colors.Blue;

    SolidColorBrush routeBrush = new SolidColorBrush(routeColor);

    MapPolyline routeLine = new MapPolyline();

    routeLine.Locations = new LocationCollection();

    routeLine.Stroke = routeBrush; routeLine.Opacity = 0.65;

    routeLine.StrokeThickness = 5.0;

    // Retrieve the route points that define the shape of the route.

    foreach (Location p in e.Result.Result.RoutePath.Points)


    routeLine.Locations.Add(new Location(p.Latitude, p.Longitude));


    // Add a map layer in which to draw the route. MapLayer myRouteLayer = new MapLayer();

    mMap.Children.Add(myRouteLayer); // Add the route line to the new layer. myRouteLayer.Children.Add(routeLine);

    // Figure the rectangle which encompasses the route. This is used later to set the map view. LocationRect rect = new LocationRect(routeLine.Locations[0], routeLine.Locations[routeLine.Locations.Count - 1]);

    // For each geocode result (which are the waypoints of the route), draw a dot on the map.

    foreach (GeocodeService.GeocodeResult gr in geocodeResults)


    Ellipse point = new Ellipse(); point.Width = 10;

    point.Height = 10; point.Fill = new SolidColorBrush(Colors.Red);

    point.Opacity = 0.65;

    Location location = new Location(gr.Locations[0].Latitude, gr.Locations[0].Longitude);

    MapLayer.SetPosition(point, location);

    MapLayer.SetPositionOrigin(point, PositionOrigin.Center);

    // Add the drawn point to the route layer. myRouteLayer.Children.Add(point);


    // Set the map view using the rectangle which bounds the rendered route. mMap.SetView(rect);



    private void resetBtn_Click(object sender, RoutedEventArgs e)


    this.txtEnd.Text = "";

    this.txtStart.Text = "";


    Monday, July 12, 2010 11:25 PM
  • Hi,

       You can check the other thread of yours


       It gets more members participated.  

    Best Regards 

    Tuesday, July 13, 2010 5:48 AM