locked
C# Google Maps API with Routing - use routing sequence in grid RRS feed

  • Question

  • Hello,

    I have a c# web app where I am pinning locations to a google map, and routing the pins.  I would like to use the route sequence from the google maps API to update my dataset.  I am creating my google map by writing the javascript into a stringbuilder then assigning the string builder to a literal on the client side.  

     private void LoadMap()
            {
                SqlConnection conn = new SqlConnection();
                SqlCommand cmd = new SqlCommand();
                string connStr = ConfigurationManager.ConnectionStrings["FurnitureDB"].ConnectionString;
                conn.ConnectionString = connStr;
                conn.Open();
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Connection = conn;
                cmd.CommandText = "GetGooglePins_active";
                SqlDataReader datareader = cmd.ExecuteReader();
                var dt = new DataTable();
                dt.Load(datareader);
    
                //
                StringBuilder sb = new StringBuilder();
    
                // sb.Append("<script>");
                sb.Append("<script type='text/javascript'>");
    
                sb.Append(" function initMap() {");
                //sb.Append("var waypoints[];");
                sb.Append("   var directionsService = new google.maps.DirectionsService;");
                sb.Append("   var directionsDisplay = new google.maps.DirectionsRenderer;");
                //sb.Append("directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});");
                sb.Append("   var map = new google.maps.Map(document.getElementById('map'), { zoom: 7, center: { lat: 41.85, lng: -87.65} });");
                sb.Append("directionsDisplay.setMap(map);");
    
                sb.Append("var onChangeHandler = function() {");
                sb.Append("    calculateAndDisplayRoute(directionsService, directionsDisplay);");
                sb.Append("};");
                sb.Append("document.getElementById('start').addEventListener('change', onChangeHandler);");
                sb.Append("document.getElementById('end').addEventListener('change', onChangeHandler);");
                sb.Append("directionsService.route({");
               
    
                sb.Append("   origin: '40.572530, -81.853254',");
                sb.Append(" destination: 'ventura, CA',");
                sb.Append(" waypoints: [");
                foreach (DataRow dr in dt.Rows)
                {
                    string latitude = dr["latitude"].ToString();
                    string longitude = dr["longitude"].ToString();
                    sb.Append("{location: '" + latitude + ", " + longitude + "',");
                    
                    sb.Append("     stopover: true");
                    sb.Append("  },");
                   
                }
                sb.Append("],");
                sb.Append("   optimizeWaypoints: true,");
                sb.Append("travelMode: 'DRIVING'");
                
                sb.Append(" }, function(response, status)");
                sb.Append(" {");
                sb.Append(" if (status === 'OK')");
                sb.Append(" {");
                sb.Append("     directionsDisplay.setDirections(response);");
                /***************************************************************/
                sb.Append("var route = response.routes[0];");
                sb.Append(" var summaryPanel = document.getElementById('directions-panel');");
                sb.Append("summaryPanel.innerHTML = '';");
                // For each route, display summary information.
                sb.Append("for (var i = 0; i < route.legs.length; i++)");
                sb.Append(" {");
                sb.Append("var routeSegment = i + 1;");
                sb.Append(" summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment +");
                sb.Append("       '</b><br>';");
                sb.Append("summaryPanel.innerHTML += route.legs[i].start_address + ' to ';");
                //can I capture these addresses and use the sequence numbers?
                sb.Append("summaryPanel.innerHTML += route.legs[i].end_address + '<br>';");
                sb.Append("summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';");
                sb.Append("}");
                /***********************************************************************/
                sb.Append(" }");
                sb.Append(" else");
                sb.Append(" {");
                sb.Append("    window.alert('Directions request failed due to ' + status);");
                sb.Append(" }");
                sb.Append(" })");
                sb.Append(" };");
    
                sb.Append("function calculateAndDisplayRoute(directionsService, directionsDisplay) {");
    
                sb.Append("directionsService.route({");
                sb.Append("   origin: 'millersburg,oh',");
                sb.Append(" destination: 'ventura, CA',");
                sb.Append("travelMode: 'DRIVING'");
                sb.Append(" }, function(response, status)");
                sb.Append(" {");
                sb.Append(" if (status === 'OK')");
                sb.Append(" {");
                sb.Append("     directionsDisplay.setDirections(response);");
                sb.Append(" }");
                sb.Append(" else");
                sb.Append(" {");
                sb.Append("    window.alert('Directions request failed due to ' + status);");
                sb.Append(" }");
                sb.Append(" }");
                sb.Append(")};");
                sb.Append("</script>");
    
                Literal1.Text = sb.ToString();
                LoadsGridDataBind();
    

    I have a summary panel that shows the sequence of each stop.  I would like to use those sequence numbers to update my database accordingly.  How can I get those values back to my c# code? 

    *Not allowed to add images yet, I will when able. 

    Thanks for any ideas.  

    • Moved by CoolDadTx Tuesday, October 17, 2017 3:51 PM ASP.NET related
    Tuesday, October 17, 2017 2:50 AM

All replies

  • Please post questions related to web development in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, October 17, 2017 3:50 PM