Find Pub Near Your Location Through ASP.Net MVC

Introduction

This article explains how to create a Search Finder using ASP.NET MVC that can help to find a Pub near your location.

Step 1

First of all you need to create a database where Pubs are to be stored.

You can use the database that are attached with my application code and then can be attached to your application.

pub finder1.jpg

Now we will add a Controller Class in the Controller in which an Action will return the View.

It's coding will be like this:

 

  1. public ActionResult StoreLocator(string address)  
  2. {  
  3.     // Make sure we have an address  
  4.     if (string.IsNullOrEmpty(address))  
  5.         return View();  
  6.     var output = GoogleMapsAPIHelpersCS.GetGeocodingSearchResults(address);  
  7.     var rsltCnt = output.Elements("result").Count();  
  8.     if (rsltCnt == 0)  
  9.     {  
  10.         // No matching address found!  
  11.         ViewData["NoResults"] = true;  
  12.         return View();  
  13.     }  
  14.     else if (rsltCnt == 1)  
  15.     {  
  16.         // Got back exactly one result, show it!  
  17.         return RedirectToAction("StoreLocatorResults"new { Address = output.Element("result").Element("formatted_address").Value });  
  18.     }  
  19.     else  
  20.     {  
  21.         // Got back multiple results - We need to ask the user which address they mean to use...  
  22.         var same = from result in output.Elements("result")  
  23.                       let formatted_address = result.Element("formatted_address").Value  
  24.                       select formatted_address;  
  25.         ViewData["Matches"] = same;  
  26.         return View();  
  27.     }  
  28. }  

 

In this code, first the application will check whether the user provided an address or not, if not then it will return the user to the view where he will see the same page and will need to provide the address or name of city.

If he has provided an address then it will use the Google's Geolocator Service and will return one of the Pubs at that location, if it finds that the address provided by the user is not understandable then it will ask the user to provide an understandable address or city name.

Step 2

Now we will add an Index File to the application where a form will be created, on this form the user will find a TextBox and a Button, in this TextBox the user will be asked to provide the location.

 

  1. <% using (Html.BeginForm())  
  2.    { %>  
  3.     <p>  
  4.         <b>Enter Something:</b>  
  5.         <%: Html.TextBox("address")%>  
  6.         <input type="submit" value="Search!" />  
  7.         <br />  
  8.         <i>Example: Delhi, India</i> or <i>011</i> or <i>Shastri Nagar, Ghaziabad</i>  
  9.     </p>  
  10.     <% if (ViewData["NoResults"] != null) { %>  
  11.             <div class="input-validation-error">  
  12.  The Address you are providing is not understood please type your city name or zip code.  
  13.             </div>  
  14.     <% } %>  
  15.     <%  
  16.     var possibleMatches = ViewData["Matches"] as IEnumerable<string>;  
  17.     if (possibleMatches != null) { %>  
  18.         <div style="padding-left: 25px; margin-top: 10px;">  
  19.             <b>Did you mean...</b>  
  20.             <ol>  
  21.                 <% foreach (var find in possibleMatches) { %>  
  22.                     <li><%: Html.ActionLink(find, "PubFinderResults"new { Address = find }) %></li>  
  23.                 <% } %>  
  24.             </ol>  
  25.         </div>  

Step 3

We will also create a default index file that will ask the user whether he is looking for a pub! If yes then he will be taken to the index file that we had created previously.

 

  1. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
  2.     <h2>Looking for a Pub! Try the Pub Finder</h2>  
  3.     <p>  
  4.         <%: Html.ActionLink("Looking for a Pub!""StoreLocator") %>  
  5.     </p>  
  6. </asp:Content>  

 

Output

It's output will be like the following.

First of all you need to click on one of the links that will take you to the Search Finder.

pub finder2.jpg

Now on the Next Page provide the name of the city or the address where you want to find a Pub and then click on the "Search" button. If there are many places similar to the place that you are looking for then it will provide all the similar locations and then you can choose one of them.

pub finder3.jpg

You can download the code that is provided above.


Similar Articles