Raize Ahamed Raize Ahamed - 3 months ago 19
C# Question

How to draw own path for two points in google map and find the distance between them?

I am developing a windows form application in .NET

Now I am in need of including this maps module in my application. I am very new to maps API. This is what is my requirements. I am having latitude, longitude position of all points found from google maps.

Using this latitude, longitude position of two points, map should show me a shortest path and its road distance or driving distance (not circle distance). If the user wants to change the route path, he should be able to change the path in map and distance has to be calculated again.

How do I do this in my application? I tried searching a lot in google, I couldn't able to find as per my need.

If it is already available in internet anywhere, please give me the link.

I want to use this module only for getting distance of my own route between two points.

Please guide me. I am ready to learn.

Answer

You can achieve your goal by using GDS Google Map WinForms Control. Here is how:

  1. Drag the GdgGoogleMap control on your form, name it as _gdsGoogleMap and set the Dock property to "Fill";

  2. Create two event handlers: Map Initialized Event: GdsGoogleMapMapInitializedEventHandler Map Directions Service Completed Event: GdsGoogleMapMapDirectionsServiceCompletedEventHandler

  3. Code the followings:

    using System.Windows.Forms;    
    using GdsGoogleMap.DirectionsService;    
    using GdsGoogleMap.DirectionsService.Results;    
    using GdsGoogleMap.MapEvents;    
    
    namespace SimpleMap    
    {    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void GdsGoogleMapMapInitializedEventHandler(object sender, MapInitializedEventArgs e)
            {
                var dReq = new DirectionsRequest
                {
                    Origin = new AddressLocation( "University of Calgary, Calgary, AB" ),
                    Destination = new AddressLocation( "SAIT, Calgary, AB " ),
                    TravelMode = TravelModeOption.Driving, 
                };
    
                _gdsGoogleMap.BeginDirectionsService( dReq );
            }
    
            private void GdsGoogleMapMapDirectionsServiceCompletedEventHandler( object sender, MapDirectionsServiceCompletedEventArgs e )
            {
                if ( e.Status != DirectionsQueryStatusOption.Ok || e.Result == null )
                {
                    return;
                }
    
                var distance = 0;
    
                foreach (var route in e.Result.RouteCollection )
                {
                    if (route == null)
                    {
                        continue;
                    }
    
                    foreach (var leg in route.LegCollection)
                    {
                        if (leg == null )
                        {
                            continue;
                        }
    
                        distance = leg.Distance.Value;
                    }
                }
    
                Text = distance + "m";
            }
        }
    }
    
  4. Compile and run the program and you will see a route automatically added on the map and the distance displayed on the Form Title.

If you prefer to pass the (lat, lng) as the start and end, please use

Origin = new LatLngLocation( lat, lng )

Please note that 1. the route is determined by Google 2. if you want to change the path, you will need to change the Origin and Destination, then do the direction service again. Or you need to add the way point collection and set optimize way point to true.