public void GeoCode() { // in order to reduce the number of geocodes that we send, let's group all uploaded entries by address // so that all those living in an apartment of the same address might be geocoded with one call if (CanGeoCode()) { List<string> distinctAddresses = GetDistinctAddressesInBatch(); var geocoder = new GeocodingProxy(); Dictionary<string, Coordinate> geocodedAddresses = new Dictionary<string, Coordinate>(); foreach (string distinctAddress in distinctAddresses) { try { var matchingIAddress = UploadedAddresses.Where(x => x.FullAddressWithCity == distinctAddress).First(); var coordinates = geocoder.Geocode(matchingIAddress); geocodedAddresses.Add(distinctAddress, coordinates); } catch (Exception ex) { } } // save coordinates in the database foreach (var geocodedAddress in geocodedAddresses) { string addressString = geocodedAddress.Key; var coordinates = geocodedAddress.Value; // find all entries with same address var recordsWithSameAddress = (from a in this.UploadedAddresses where a.FullAddressWithCity == addressString select a).ToList(); // update each record with associated coordinates foreach (var address in recordsWithSameAddress) { address.UpdateCoordinates(coordinates); } } } else { throw new Exception("Cannot geocode batch (not enought geocoding available)."); } }
private bool Geocode(ref string lng, ref string lat) { bool ret = false; // PAOLO WAS HERE throw new NotImplementedException("Must revisit"); City thisCity = new City(int.Parse(DropDownListCities.SelectedValue)); string address = this.TextBoxHouseNumber.Text + " " + TextBoxStreet.Text + ", " + DropDownListCities.SelectedItem.Text + " " + thisCity.StateProvince.Description; try { var geocoder = new GeocodingProxy(); Coordinate coords = geocoder.Geocode(address); lat = coords.Latitude.ToString(); lng = coords.Longitude.ToString(); ret = true; } catch (Exception ex) { } return ret; }
public bool Geocode() { bool ret = false; CMS.City city = CMS.City.FetchByID(CityId); string address = FullAddress; try { var geocoder = new GeocodingProxy(); var coords = geocoder.Geocode(this); this.Lat = coords.Latitude.ToString(); this.Long = coords.Longitude.ToString(); var db = new TerritoryDBDataContext(); db.sp_Address_Update_Coordinates(AddressId, Long, Lat); ret = true; } catch (Exception ex) { } return ret; }