Exemple #1
0
            public void GetDistances()
            {
                if (tblResults != null && tblResults.Rows.Count > 0)
                {
                    //Add a new column to the table specific to Google so we don't step on any of the original data
                    DataColumn googleColumn = new DataColumn("GoogleDistance");
                    tblResults.Columns.Add(googleColumn);

                    //Setup the string we'll use as the origin Lat/Lng for Google
                    String originString = String.Format("{0},{1}", lat, lng);

                    //Setup a list of Lat/Lng destinations to loop through based off of the data from the databse
                    List <String> destinations = new List <String>();
                    for (Int32 i = 0; i < tblResults.Rows.Count; i++)
                    {
                        destinations.Add(String.Format("{0},{1}",
                                                       tblResults.Rows[i]["Latitude"].ToString(),
                                                       tblResults.Rows[i]["Longitude"].ToString()));
                    }

                    //Call the GoogleHelper with what we've put together and store the results in a String array
                    String[] distances = GoogleHelper.GetDistances(originString, destinations.ToArray());

                    //Loop through the rows in the data table again and update with what we received from Google
                    for (Int32 i = 0; i < tblResults.Rows.Count; i++)
                    {
                        if ((i + 1) <= distances.Length)
                        {
                            //If we received a distance for this destination...
                            if (distances[i] != "")
                            {
                                //Update our Distance with Google's for Display
                                tblResults.Rows[i]["Distance"] = distances[i].ToString();
                                //Update our Numeric Distance with Google's for sorting
                                tblResults.Rows[i]["NumericDistance"] = Convert.ToDouble(distances[i].Replace(" mi", String.Empty).Trim());
                            }
                            else
                            {
                                //Update our Distance with our calculated Numeric Distance as a fallback
                                tblResults.Rows[i]["Distance"] = String.Format("{0:##0.0} mi",
                                                                               tblResults.Rows[i]["NumericDistance"]);
                            }
                        }
                        else
                        {
                            //Update our Distance with our calculated Numeric Distance as a fallback
                            tblResults.Rows[i]["Distance"] = String.Format("{0:##0.0} mi",
                                                                           tblResults.Rows[i]["NumericDistance"]);
                        }
                    }
                }
            }