What do we want to achieve,
- Create a Google Maps AutoComplete dropdown.
- Save those address details into our database Server (here, I am using SQL Server).
- Retrieve those map details into the browser, create and place markers on the map.
- Navigate between markers on mouse click.
Before creating the project, I will show you how to get API key for Google Maps API.
Step 1.0
Create a Google Maps API from Google console application
Go to Console Google.
Create New Project.

It will take some time to create the project.
Click Google Maps Javascript API link at the right side under Google Maps APIs.

Click "Enable" to enable API.

Once you enable, you will see one button “Create Credential”.

Copy your credentials somewhere.
Step 1.1
Create an MVC project and name it as GoogleMapTutor.
Go to Add > New Project > ASP.NET Web Application.

Select MVC with No Authentication (As we don't need authentication).
Right click on References and click "Manage NuGet Packages".

Add packages AngularJS.Core and Entity Framework.


Step 2
Create Place.cs and MapDbContext.cs files into "Models" folder.
Add the code given below inside Place.cs file.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
- namespace GoogleMapTutor.Models {
- public partial class Place {
- [Key]
- public Guid Id {
- get;
- set;
- }
- public string PlaceGroupId {
- get;
- set;
- }
- public string PlaceId {
- get;
- set;
- }
- public string FullAddress {
- get;
- set;
- }
- public string Latitude {
- get;
- set;
- }
- public string Longitude {
- get;
- set;
- }
- }
- }
Add the code into MapDbContext.cs file.
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Web;
- namespace GoogleMapTutor.Models {
- public class MapDbContext: DbContext {
- public MapDbContext(): base("GoogleMapConnString") {
- Database.SetInitializer < MapDbContext > (new DropCreateDatabaseIfModelChanges < MapDbContext > ());
- }
- public DbSet < Place > places {
- get;
- set;
- }
- }
- }
The code given above is to generate a database, using Entity Framework Code-First Approach.
Step 3
Add the connection string into Web.config file.
Add the code given below for the database connection string. You do not have localDB installed but you can change the connection string to sqlserver db.
- <connectionStrings>
- <add name="GoogleMapConnString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
- </connectionStrings>
Step 4
Add a Controller into Controllers > PlacesController and paste the code given below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using GoogleMapTutor.Models;
- namespace GoogleMapTutor.Controllers {
- public class PlacesController: Controller {}
- }
Add 4 action methods given below into the Controller class.
- MapDbContext db = new MapDbContext();
- public ActionResult Index() {
- //var result = db.places.GroupBy(x => x.PlaceGroupId).Select(grp => grp.ToList()).ToList();
- var items = new List < SelectListItem > ();
- var groups = db.places.Select(x => new SelectListItem {
- Text = x.PlaceGroupId, Value = x.PlaceGroupId
- }).Distinct().ToList();
- if (groups != null && groups.Count > 0) {
- groups[0].Selected = true;
- }
- return View(groups);
- }
- [HttpGet]
- public ActionResult Add() {
- return View();
- }
- [HttpPost]
- public ActionResult Add(List < Place > places) {
- places.ForEach(x => x.Id = Guid.NewGuid());
- db.places.AddRange(places);
- var result = db.SaveChanges();
- if (result > 0) {
- return Json(new {
- result = "Redirect", url = Url.Action("Index", "Places")
- });
- }
- return Json(new {
- result = "InvalidLogin"
- }, JsonRequestBehavior.AllowGet);
- }
- [HttpGet]
- public JsonResult GetLocationsByGroupId(string groupid) {
- var locations = db.places.Where(x => x.PlaceGroupId == groupid).ToList();
- return Json(locations, JsonRequestBehavior.AllowGet);
- }
The first is Method Index() which returns List<SelectListItem>() for dropdown.
The last method is to get all the places, which are based on its GroupId. This function will return JSON data.
Step 5
Create two files “Add.cshtml” and “Index.cshtml” into Into Views>Places folder (Create Places folder, if it does not exist).
Note
Please make sure that in Layout.cshtml page, jQuery and Bootstrap are referred.
Step 6
Open “Add.cshtml” file
Remove all the code from the page and paste the code given below.
Replace the credentials used for the Application and paste your own credentials into Google API script tag given below.
- @ {
- ViewBag.Title = "Add";
- } < div ng - app = "googleMapApp"
- ng - controller = "googleMapController"
- ng - cloak id = "angular_scope" > < div style = "margin:70px 0;" > < /div> < div class = "row"
- ng - show = "IsLoaded" > < div class = "col-xs-12" > < h4 > Add Places < /h4> < table class = "table table-bordered table-hover table-striped" > < thead > < tr > < th class = "col-sm-2" > Sl No < /th> < th > Id < /th> < th > Full Address < /th> < th > Latitude < /th> < th > Longitude < /th> < td > Action < /td> < /tr> < /thead> < tbody > < tr > < td > < /td> < td colspan = "6" > < span google - map - directive placelist = "placeList"
- groupid = "groupId" > < /span> < /td> < /tr> < tr ng - repeat = "place in placeList" > < td > {
- {
- $index + 1
- }
- } < /td> < td > {
- {
- place.PlaceId
- }
- } < /td> < td > {
- {
- place.FullAddress
- }
- } < /td> < td > {
- {
- place.Latitude
- }
- } < /td> < td > {
- {
- place.Longitude
- }
- } < /td> < td > < input type = "button"
- class = "btn btn-xs btn-primary"
- ng - click = "remove(place)"
- value = "delete" / > < /td> < /tr> < /tbody> < tfoot > < tr > < td colspan = "6" > < div class = "text-right" > < input type = "text"
- placeholder = "Enter Group Id"
- class = "input-sm"
- ng - model = "groupId" / > < input type = "button"
- class = "btn btn-primary"
- ng - click = "post()"
- value = "Add Places" / > < /div> < /td> < /tr> < /tfoot> < /table> < /div> < /div> < /div>
- @section scripts { < script src = "https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false&key=AIzaSyD3fJEYrzU3pvnZ_DGwBN0yxM-e7fCbNjI" > < /script> < script src = "~/Scripts/angular.js" > < /script> < script src = "~/Scripts/app/add.js" > < /script>
- }


Join the conversation! Your thoughts help the community grow.