private void EndAsync(IAsyncResult ar) { TerraService terra = (TerraService)ar.AsyncState; PlaceFacts facts = terra.EndGetPlaceFacts(ar); this.LA.Text = String.Format("Latitude: {0:0.##}", facts.Center.Lat); this.LO.Text = String.Format("Longitude: {0:0.##}", facts.Center.Lon); }
private IAsyncResult BeginAsync(object sender, EventArgs e, AsyncCallback cb, object state) { TerraService terra = new TerraService(); Place place = new Place(); place.City = "Seattle"; place.State = "WA"; place.Country = "US"; IAsyncResult ar = terra.BeginGetPlaceFacts(place, cb, terra); return(ar); }
public static void initTerraService() { if (ts == null && Project.serverAvailable) { int retries = 0; while (retries <= maxRetries) { try { ts = new TerraService(); //ts.Url = "http://" + Project.TERRA_SERVER + "/TerraService.asmx"; break; } catch (Exception e) { retries++; if (retries == maxRetries) { LibSys.StatusBar.Error("Unable to establish a connection to the TerraServer Web Service" + e.Message); MessageBox.Show(Project.mainForm, "Unable to establish a connection to the TerraServer Web Service" + e.Message, "TerraService Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch { retries++; if (retries == maxRetries) { LibSys.StatusBar.Error("TerraServer Web Service protocol error during initialization"); MessageBox.Show(Project.mainForm, "TerraServer Web Service protocol error during initialization", "TerraService Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } } }
/// <summary> The main entry point for the application. </summary> static void Main(string[] args) { // Create the GPS point from your location services data LonLatPt location = new LonLatPt(); // Modify Lat and Lon based on your needs // This example uses the GPS Coordinates for "Eau Claire, Wisconsin, United States" location.Lat = 44.811349; location.Lon = -91.498494; // Create a new TerraService object TerraService ts = new TerraService(); // Output the nearest location from the TerraService Console.WriteLine(ts.ConvertLonLatPtToNearestPlace(location)); // For console app to stay open/close easily Console.WriteLine("Press any key to close window..."); Console.ReadKey(); // Lastly, appreciate the Microsoft folks that made this available for free // They are all interesting individuals but you should read about Jim Gray via Wikipedia to // understand some history behind this cool web service. }
public static void GetCityData(int addressID) { try { using (SqlConnection cn = new SqlConnection("context connection=true")) { cn.Open(); string selectQuery = @"SELECT a.City, s.Name As State, c.Name As Country FROM Person.Address a INNER JOIN Person.StateProvince s ON a.StateProvinceID = s.StateProvinceID INNER JOIN Person.CountryRegion c ON s.CountryRegionCode = c.CountryRegionCode WHERE a.AddressID = @addressID"; SqlCommand selectCmd = new SqlCommand(selectQuery, cn); selectCmd.Parameters.AddWithValue("@addressID", addressID); SqlDataReader reader = selectCmd.ExecuteReader(); if (reader.HasRows) { reader.Read(); string city = (string)reader[0]; string state = (string)reader[1]; string country = (string)reader[2]; reader.Close(); string placeName = city + ", " + state + ", " + country; string insertQuery = "INSERT INTO Person.CityDetails VALUES (@addressID, @name, @longitude, @latitude, @population, @image)"; SqlCommand insertCmd = new SqlCommand(insertQuery, cn); SqlParameter addressIDParam = new SqlParameter("@addressID", SqlDbType.Int); SqlParameter nameParam = new SqlParameter("@name", SqlDbType.NVarChar, 256); SqlParameter longParam = new SqlParameter("@longitude", SqlDbType.Float); SqlParameter latParam = new SqlParameter("@latitude", SqlDbType.Float); SqlParameter popParam = new SqlParameter("@population", SqlDbType.Int); SqlParameter imgParam = new SqlParameter("@image", SqlDbType.Image); insertCmd.Parameters.AddRange(new SqlParameter[] { addressIDParam, nameParam, longParam, latParam, popParam, imgParam }); addressIDParam.Value = addressID; TerraService terraService = new TerraService(); PlaceFacts[] places = terraService.GetPlaceList(placeName, 100, false); foreach (PlaceFacts facts in places) { LonLatPt coords = facts.Center; TileMeta metadata = terraService.GetTileMetaFromLonLatPt(coords, 1, Scale.Scale8m); byte[] image = terraService.GetTile(metadata.Id); nameParam.Value = facts.Place.City; longParam.Value = coords.Lon; latParam.Value = coords.Lat; popParam.Value = facts.Population; imgParam.Value = image; try { insertCmd.ExecuteNonQuery(); } catch (Exception e) { SqlContext.Pipe.Send("Cannot insert row for place " + facts.Place.City); SqlContext.Pipe.Send(e.Message); } } cn.Close(); SqlContext.Pipe.Send("Command executed successfully."); terraService.Dispose(); } else { reader.Close(); cn.Close(); SqlContext.Pipe.Send("No addresses in the database match the specified ID."); } } } catch (Exception e) { SqlContext.Pipe.Send("An error occurred executing the GetCityData stored procedure:"); SqlContext.Pipe.Send(e.Message); } }