Geocode

 

 

        private void btnGeocode_Click(object sender, EventArgs e)
        {
            btnGeocode.Enabled = false;
            try
            {
                //create request if null
                if (geocodeReqest == null)
                {
                    geocodeReqest = new TestInternal.ts.GeocodeSpecification();

                    //Create an 2 array of locations
                    geocodeReqest.Addresses = new TestInternal.ts.Address[2];

                    TestInternal.ts.Location locA = new TestInternal.ts.Location();
                    locA.Name = "Stop A";
                    locA.Address = new ts.Address();
                    locA.Address.Street = "6718 Whittier Avenue";
                    locA.Address.City = "McLean";
                    locA.Address.State = "VA";
                    locA.Address.PostalCode = "22101";
                    geocodeReqest.Addresses[0] = locA.Address;

                    TestInternal.ts.Location locB = new TestInternal.ts.Location();
                    locB.Name = "Stop B";
                    locB.Address = new ts.Address();                   
                    locB.Address.Street = "1450 Old Chain Bridge Road";
                    locB.Address.City = "McLean";
                    locB.Address.State = "VA";
                    locB.Address.PostalCode = "22101";
                    geocodeReqest.Addresses[1] = locB.Address;

                    geocodeReqest.IsNeedMatchCode = true;
                }
                try
                {
                    client.Timeout = 1000 * 60 * 30;//30 minutes
                    DateTime dt = DateTime.Now;
                    TestInternal.ts.GeocodeResult result = client.Geocode(geocodeReqest);
                    TimeSpan tsp = DateTime.Now.Subtract(dt);
                    if (result.Status == TestInternal.ts.OperationStatus.Success)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("Total Time:{0}\r\n\r\n", tsp);
                        //show result
                        for (int i = 0; i < result.Items.Length; i++)
                        {
                            TestInternal.ts.GeocodeItem item = result.Items[i];
                            if (item.Locations != null)
                            {
                                TestInternal.ts.Location loc = item.Locations[0];
                                if (loc != null)
                                    sb.AppendFormat("{0}\t {1}\t [{2:f5},{3:f5}]\r\n", loc.Name, loc.MatchCode, loc.LatLong.Latitude, loc.LatLong.Longitude);
                            }
                        }
                        txtResult.Text = sb.ToString();
                        return;
                    }
                    else//(result.Status == WebRef.ts.OperationStatus.SuccessWithErrors) 
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("Total Time:{0}\r\n\r\n", tsp);
                        sb.AppendFormat("Status:{0}\r\n", result.Status);
                        foreach (TestInternal.ts.Error i in result.Errors)
                        {
                            sb.AppendFormat("{0}\r\n", i.Message);
                        }
                        txtResult.Text = sb.ToString();
                        //Show result with error. Some of locations can be skipped or may heppens some errors.
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            finally
            {
                btnGeocode.Enabled = true;
            }
        }