public async void CreateAsAsync(object document) { if (!_es.IndexExists(Indices.Index(ES_INDEX_NM)).Exists) { CreateMapping(_es); } Store store = (Store)document; if (store.Longitude == 0 || store.Latitude == 0) { string address = String.Format("{0},{1},{2}-{3}, US", store.Addr_Ln_1, store.City, store.State, store.Zip); GoogleGeocoding_Location location = GeneralPurpose.GetLatLong(address); store.Latitude = location.lat; store.Longitude = location.lng; } ESIndex_Store_Document store_to_add = new ESIndex_Store_Document { Id = store.Id, Name = store.Name, Location = new GeoLocation(store.Latitude ?? 0, store.Longitude ?? 0), City = store.City, State = store.State, Retailer = new ESIndex_Store_Document_Retailer { Id = store.RetailerId, Name = store.Retailer.Name, Agg_Name = String.Format("{0}={1}", store.Retailer.Name, store.RetailerId) } }; store_to_add.Clients = store.ClientStores.Select(s => new ESIndex_Store_Document_Client { UrlCode = s.Client.UrlCode, Name = s.Client.Name, CreatedAt = s.CreatedAt }).ToArray <ESIndex_Store_Document_Client>(); await _es.IndexAsync(store_to_add, i => i .Index(ES_INDEX_NM) .Type(ES_INDEX_TYP_NM) ); }
public static GoogleGeocoding_Location GetLatLong(string address) { GoogleGeocoding_Location retval = new GoogleGeocoding_Location(); string requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyA47sqq4fHReCXHT3UTFGaLgulC5sooxt8&address={0}&sensor=false", Uri.EscapeDataString(address)); var request = (HttpWebRequest)WebRequest.Create(requestUri); request.Method = "GET"; request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; request.Referer = "http://localhost:1273/dummy"; var response = (HttpWebResponse)request.GetResponse(); string content = String.Empty; using (var stream = response.GetResponseStream()) { using (var sr = new StreamReader(stream)) { content = sr.ReadToEnd(); } } try { var results = (JArray)((JObject.Parse(content))["results"]); var attribs = results.First; retval.lat = (double)attribs["geometry"]["location"]["lat"]; retval.lng = (double)attribs["geometry"]["location"]["lng"]; } catch { retval.lat = 0; retval.lng = 0; } return(retval); }