private string RetrieveArborCoordinates(Address address) { // const int gmapsRequestsMaxNumberPerSecond = 10; const string gmapsApiUrl = "https://maps.googleapis.com/maps/api/geocode/xml"; var homepage = Umbraco.TypedContentSingleAtXPath("/root/Homepage"); if (homepage == null) { return String.Empty; } var requestKeyParam = homepage.GetPropertyValue<string>("gmapsApiKey"); var requestAddressParam = address.GetFormattedAddressLabel().Replace("<br />", ", "); var requestUrl = String.Format("{0}?key={1}&address={2}", gmapsApiUrl, requestKeyParam, requestAddressParam); var responseXml = XDocument.Load(requestUrl); var xmlNode = responseXml.XPathEvaluate("/GeocodeResponse/status/text()") as IEnumerable<object>; if (xmlNode == null) { return String.Empty; } var responseStatus = xmlNode.Cast<XText>().First().Value; if (responseStatus != "OK") { return String.Empty; } xmlNode = responseXml.XPathEvaluate("//lat/text()") as IEnumerable<object>; var lat = (xmlNode != null) ? xmlNode.Cast<XText>().First().Value : String.Empty; xmlNode = responseXml.XPathEvaluate("//lng/text()") as IEnumerable<object>; var lon = (xmlNode != null) ? xmlNode.Cast<XText>().First().Value : String.Empty; return (!String.IsNullOrEmpty(lat) && !String.IsNullOrEmpty(lon)) ? String.Format("{0},{1}", lat, lon) : String.Empty; }
private void SaveArborAddress(int arborId, Address arborAddress) { JObject updatedAddress = null; var pageContent = Services.ContentService.GetById(arborId); var propertyValue = pageContent.GetValue<string>("meetingAddress"); if (!String.IsNullOrEmpty(propertyValue)) { updatedAddress = (JObject) JsonConvert.DeserializeObject<List<object>>(propertyValue).FirstOrDefault() ?? new JObject(); } updatedAddress = updatedAddress ?? new JObject(); updatedAddress.Merge(JObject.FromObject(arborAddress)); pageContent.SetValue("meetingAddress", JsonConvert.SerializeObject(new[] { updatedAddress })); if (pageContent.IsDirty()) { Services.ContentService.SaveAndPublishWithStatus(pageContent); } }
private Address PopulateArborAddress(IPublishedContent arbor) { var address = new Address(); var addressContent = arbor.GetPropertyValue<IPublishedContent>("meetingAddress"); if (addressContent != null) { address.Street = (addressContent.GetPropertyValue<string>("street") ?? String.Empty).Trim(); address.City = (addressContent.GetPropertyValue<string>("city") ?? String.Empty).Trim(); address.State = (addressContent.GetPropertyValue<string>("state") ?? String.Empty).Trim(); address.ZipCode = (addressContent.GetPropertyValue<string>("zipCode") ?? String.Empty).Trim(); address.GeoCoordinates = (addressContent.GetPropertyValue<string>("geoCoordinates") ?? String.Empty).Trim(); } if ((!address.HasEmptyFields() && !String.IsNullOrWhiteSpace(address.GeoCoordinates)) || UnresolvedAddresses.Contains(arbor.Id)) { return address; } if (address.HasEmptyFields()) { int arborNumber; if (int.TryParse(arbor.GetPropertyValue<String>("arborNumber"), out arborNumber)) { var entity = GleanerContext.Current.Services.ArborService.GetArborAddress(arborNumber); if (entity != null) { address.Street = String.IsNullOrWhiteSpace(address.Street) ? entity.Street : address.Street; address.City = String.IsNullOrWhiteSpace(address.City) ? entity.City : address.City; address.State = String.IsNullOrWhiteSpace(address.State) ? entity.State : address.State; address.ZipCode = String.IsNullOrWhiteSpace(address.ZipCode) ? entity.ZipCode : address.ZipCode; address.GeoCoordinates = String.IsNullOrWhiteSpace(address.GeoCoordinates) ? entity.GeoCoordinates : address.GeoCoordinates; } } address.Street = String.IsNullOrWhiteSpace(address.Street) ? String.Empty : address.Street.Trim(); address.City = String.IsNullOrWhiteSpace(address.City) ? String.Empty : address.City.Trim(); address.State = String.IsNullOrWhiteSpace(address.State) ? String.Empty : address.State.Trim(); address.ZipCode = String.IsNullOrWhiteSpace(address.ZipCode) ? String.Empty : address.ZipCode.Trim(); } if (String.IsNullOrWhiteSpace(address.GeoCoordinates)) { address.GeoCoordinates = RetrieveArborCoordinates(address); } if (address.HasEmptyFields() || String.IsNullOrWhiteSpace(address.GeoCoordinates)) { UnresolvedAddresses.Add(arbor.Id); } SaveArborAddress(arbor.Id, address); return address; }