private async Task <MapsResponseWrapperModel> PerformNearbySearch(string pLat, string pLng, string pCategory, string pSearchKeyword, int pSearchRadius, bool pOpenNow) { MapsResponseWrapperModel APIResponse; HttpClient _httpClient = new HttpClient(); string API_Request = string.Format("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + pLat + "," + pLng + "&radius=" + pSearchRadius + "&key=" + API_Keys.MapsAPI_Key); if (pOpenNow == true) { API_Request += "&opennow=true"; } if (pCategory != "") { API_Request += ("&type=" + pCategory); Console.WriteLine("Searching for Business type: " + pCategory); } else if (pSearchKeyword != "") { API_Request += ("&keyword=" + pSearchKeyword); Console.WriteLine("Searching for Keyword: " + pSearchKeyword); } else { APIResponse = new MapsResponseWrapperModel(); APIResponse.results = new List <MapsNearbySearchResultModel>(); return(APIResponse); } var task = await _httpClient.GetAsync(API_Request).ConfigureAwait(false); task.EnsureSuccessStatusCode(); var payload = task.Content.ReadAsStringAsync(); APIResponse = JsonConvert.DeserializeObject <MapsResponseWrapperModel>(payload.Result); return(APIResponse); }
public JsonResult MapsNearbySearch([FromQuery] string lat, [FromQuery] string lng, [FromQuery] string category, int searchRadius = 1500, bool opennow = false) { var errorJSON = new{ errorMessage = "External_API_Unreachable" }; bool mapsSearchSuccess = false; bool dbSearchSuccess = false; List <string> businessTypeList = new List <string>(); List <string> keywordSearchList = new List <string>(); MapsResponseWrapperModel compoundResponse = new MapsResponseWrapperModel(); compoundResponse.results = new List <MapsNearbySearchResultModel>(); foreach (string item in searchModel.serviceDictionary[category]["businessList"]) { businessTypeList.Add(item); } foreach (string item in searchModel.serviceDictionary[category]["keywordList"]) { keywordSearchList.Add(item); } try { try { //Perform search using Google Maps API string business_status_criteria = "OPERATIONAL"; foreach (string vBusinessType in businessTypeList) { Task <MapsResponseWrapperModel> task = Task.Run <MapsResponseWrapperModel>(async() => await PerformNearbySearch(lat, lng, vBusinessType, "", searchRadius, opennow)); task.Wait(); MapsResponseWrapperModel APIResponse = task.Result; compoundResponse.results.AddRange(APIResponse.results.Where(p => String.Equals(p.business_status, business_status_criteria, StringComparison.CurrentCulture))); } foreach (string vKeyword in keywordSearchList) { Task <MapsResponseWrapperModel> task = Task.Run <MapsResponseWrapperModel>(async() => await PerformNearbySearch(lat, lng, "", vKeyword, searchRadius, opennow)); task.Wait(); MapsResponseWrapperModel APIResponse = task.Result; compoundResponse.results.AddRange(APIResponse.results.Where(p => String.Equals(p.business_status, business_status_criteria, StringComparison.CurrentCulture))); } mapsSearchSuccess = true; } catch (Exception MapsAPIException) { Console.WriteLine("Exception in HomeController.cs -> MapsNearbySearch Google Maps API search:\n" + MapsAPIException); } //Perform Database search on Custom Added Businesses try { using (SqlConnection con = new SqlConnection(CS)) { SqlCommand cmd = new SqlCommand("spSearchOpenBusinesses", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@lat", SqlDbType.Float).Value = float.Parse(lat, CultureInfo.InvariantCulture); cmd.Parameters.Add("@lng", SqlDbType.Float).Value = float.Parse(lng, CultureInfo.InvariantCulture); cmd.Parameters.Add("@radius", SqlDbType.Int).Value = searchRadius; cmd.Parameters.Add("@businessCategory", SqlDbType.VarChar).Value = category; con.Open(); SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { MapsNearbySearchResultModel business = new MapsNearbySearchResultModel(); business.business_status = "OPERATIONAL"; business.geometry = new GeometryWrapper { location = new Coordinate { lat = double.Parse(sdr["AddressLatitude"].ToString(), CultureInfo.InvariantCulture), lng = double.Parse(sdr["AddressLongitude"].ToString(), CultureInfo.InvariantCulture) } }; business.icon = ""; business.id = ""; business.name = sdr["CompanyName"].ToString(); business.opening_hours = new OpeningHours { open_now = true }; business.place_id = ""; business.price_level = "6"; business.rating = 6; string[] categoryArray = new string[1]; categoryArray[0] = category; business.types = categoryArray; compoundResponse.results.Add(business); } } dbSearchSuccess = true; } catch (Exception DBException) { Console.WriteLine("Exception in HomeController.cs -> MapsNearbySearch Database read::\n" + DBException); } compoundResponse.status = "OK"; return(Json(compoundResponse)); } catch (Exception exc) { Console.WriteLine(exc); return(Json(compoundResponse)); } }