public async Task SaveProjectDataAsync(string color) { // validate address APIAddressResource selectedAddressResource = null; this.Color = color; try { Address newAddress = new Address(); newAddress.Address1 = this.Address1; newAddress.Address2 = this.Address2; newAddress.City = this.City; newAddress.Province = this.State; newAddress.PostalCode = this.Zip; APIAddressResource apiAddress = await AddressService.ReturnValidatedAddress(newAddress); if (apiAddress.address != null) { selectedAddressResource = apiAddress; } else { throw new InvalidAddressException(); } } catch (Exception ex) { throw new InvalidAddressException(); } Project newProject = new Project { Color = this.Color, Address1 = this.Address1, Address2 = this.Address2, City = this.City, State = this.State, Description = this.Description, Name = this.Name, Latitude = 0, Longitude = 0, Zip = this.Zip }; newProject.Latitude = Convert.ToDecimal(selectedAddressResource.point.coordinates[0]); newProject.Longitude = Convert.ToDecimal(selectedAddressResource.point.coordinates[1].ToString()); await projectRepository.Add(newProject); }
public static async Task <APIAddressResource> ReturnValidatedAddress(Address address) { APIAddressResource output = null; string queryString = ""; if (!String.IsNullOrEmpty(address.Address1)) { queryString += address.Address1.Replace(" ", "%20") + "%20"; } if (!String.IsNullOrEmpty(address.Address2)) { queryString += address.Address2.Replace(" ", "%20") + "%20"; } if (!String.IsNullOrEmpty(address.Address3)) { queryString += address.Address3.Replace(" ", "%20") + "%20"; } if (!String.IsNullOrEmpty(address.City)) { queryString += address.City + "%20"; } if (!String.IsNullOrEmpty(address.Province)) { queryString += address.Province + "%20"; } if (!String.IsNullOrEmpty(address.PostalCode)) { queryString += address.PostalCode + "%20"; } string json = await Utilities.WebServiceRequest("http://dev.virtualearth.net/REST/v1/Locations?q=" + queryString + "&o=json&key=" + GlobalConfig.MapsAPIKey); LocationRootobject rootObject = JsonConvert.DeserializeObject <LocationRootobject>(json); if (rootObject.resourceSets.Count() > 0) { var resourceSet = rootObject.resourceSets[0]; if (resourceSet.resources.Count() == 0) { throw new Exception("No matches"); } if (resourceSet.resources.Count() == 1) { var resource = resourceSet.resources[0]; if (resource.address != null && resource.entityType.ToLower() == "address") { output = resource; } else { throw new Exception("Invalid address"); } } else { throw new Exception("Multiple matches"); } } return(output); }