Objective

The objective, here, is to create a country locator in which we have SQL Server database with a single table which contains a lookup of locations of all the countries, with their associated Latitude and Longitudes. So, the table is to be populated with the following data.

Latitude

Using the above data, we will be creating a simple one page MVC web app in ASP.NET which shows a Google map with these locations plotted. There will be a search criteria box that allows the user to filter which locations to show. As they type, the pins should be dynamically plotted that match the criteria.

Some Specifications

What We Will Achieve

  1. When nothing is entered in the search box.

    Latitude

  2. When we search for countries starting with “I”.

    Latitude

Implementation Steps

Step 1

Let’s add entity data model to our ASP.NET MVC app, i.e., GoogleMap.

Latitude

Step 2

Add a map controller and add an index action to it, which will return a view with list of all the locations.

Latitude

Step 3

Let’s add a view Index as:

Latitude

And it should display all the locations with search box, as shown below:

Latitude

Step 4

Let us implement responsive search. To do so, we need to add jQuery and AJAX script files from NuGet and drag them onto view Index.

Latitude

Step 5

We need to make a jQuery based AJAX call whenever a character is entered in the search text box by tracking keyup event and sending those characters to the Action search.

Latitude

Step 6

Let’s implement the action Search and it should display a list of countries starting with the chart entered in the text box.

Latitude

Latitude

Step 7

Let us render Google maps and to do so, you need to add Google map API script file with key and create the object of google.maps.Maps, which will display the map as shown below.



Latitude

Step 8

Now we will plot the map as per the search result with a pinlball.png. To do so, we need to create the object of google.maps.LatLng for each resulted Lat and Lng and pass it to the object of google.maps.Marker to plot that location. We need to plug our code in foreach loop of Ajax call success function. And it should work as expected.



Latitude

Step 9

I need to clear the plotted locations in two scenarios, i.e., every time we make a new search and whenever the search text box is empty.

To do so, we need to keep track of all locations plotted in an array and clear those from the map before we make an AJAX call and when the search textbox is empty.

Latitude

Step 10

The complete source code is given below.

Controller Source Code

  1. Hide Copy Code
  2. using GoogleMap.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. namespace GoogleMap.Controllers {
  9. public class MapController: Controller {
  10. // GET: Map
  11. public ActionResult Index() {
  12. GoogleMapEntities GE = new GoogleMapEntities();
  13. return View(GE.Locations.ToList());
  14. }
  15. [HttpPost]
  16. public ActionResult Search(string Location) {
  17. GoogleMapEntities GE = new GoogleMapEntities();
  18. var result = GE.Locations.Where(x => x.LocationName.StartsWith(Location)).ToList();
  19. return Json(result, JsonRequestBehavior.AllowGet);
  20. }
  21. }
  22. }
Index View Source Code
  1. @model IEnumerable < googlemap.models.location >
  2. <
  3. script src = "~/Scripts/jquery-1.10.2.js" > < /script>
  4. Hide Copy Code
  5. <
  6. table >
  7. <
  8. tbody >
  9. <
  10. tr >
  11. <
  12. td valign = "top" > @Html.TextBox("txtSearch", null, new {
  13. @placeholder = 'Search A Country'
  14. }) <
  15. div id = "myData" > @foreach(var item in Model) {} <
  16. table class = "table" >
  17. <
  18. tbody >
  19. <
  20. tr >
  21. <
  22. td > @item.LocationName < /td> <
  23. /tr> <
  24. /tbody> <
  25. /table> <
  26. /div> <
  27. /td> <
  28. td valign = "top" >
  29. <
  30. div id = "googleMap"
  31. style = "width:1000px;height:600px;" > < /div> <
  32. /td> <
  33. /tr> <
  34. /tbody> <
  35. /table>