Saturday, August 10, 2019

Creating Google Maps Sample App with AngularJS and asp.net MVC

Just follow the following steps in order to create a sample google app with AngularJS and asp.net MVC:

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Basic > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add. Here I have added a database for store some location information in our database for show in the google map.

Step-3: Create a table.

Here I will create 1 table (as below) for store location information. Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used 1 table as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add. A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next > Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Create a Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add. Here I have created a controller "HomeController"

Step-6: Add new action into the controller to get the view where we will show google map

Here I have added "Index" Action into "Home" Controller. Please write this following code
  1. public ActionResult Index()
  2. {
  3. return View();
  4. }

Step-7: Add another action (here "GetAllLocation") for fetch all the location from the database.

  1. public JsonResult GetAllLocation()
  2. {
  3. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  4. {
  5. var v = dc.Locations.OrderBy(a => a.Title).ToList();
  6. return new JsonResult { Data = v, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  7. }
  8. }

Step-8: Add 1 more action (here "GetMarkerInfo") for getting google marker information from the database to show in the map.

  1. public JsonResult GetMarkerInfo(int locationID)
  2. {
  3. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  4. {
  5. Location l = null;
  6. l = dc.Locations.Where(a => a.LocationID.Equals(locationID)).FirstOrDefault();
  7. return new JsonResult { Data = l, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
  8. }
  9. }

Step-9: Add a new javascript file, will contain all the necessary logic to implement our Google Map.

Right Click on Action Method (here right click on Index action) > Add View... > Enter View Name > Select View Engine (Razor) > Add.
  1. var app = angular.module('myApp', ['uiGmapgoogle-maps']);
  2. app.controller('mapController', function ($scope, $http) {
  3.  
  4. //this is for default map focus when load first time
  5. $scope.map = { center: { latitude: 22.590406, longitude: 88.366034 }, zoom: 16 }
  6.  
  7. $scope.markers = [];
  8. $scope.locations = [];
  9.  
  10. //Populate all location
  11. $http.get('/home/GetAllLocation').then(function (data) {
  12. $scope.locations = data.data;
  13. }, function () {
  14. alert('Error');
  15. });
  16.  
  17. //get marker info
  18. $scope.ShowLocation = function (locationID) {
  19. $http.get('/home/GetMarkerInfo', {
  20. params: {
  21. locationID: locationID
  22. }
  23. }).then(function (data) {
  24. //clear markers
  25. $scope.markers = [];
  26. $scope.markers.push({
  27. id: data.data.LocationID,
  28. coords: { latitude: data.data.Lat, longitude: data.data.Long },
  29. title: data.data.title,
  30. address: data.data.Address,
  31. image : data.data.ImagePath
  32. });
  33.  
  34. //set map focus to center
  35. $scope.map.center.latitude = data.data.Lat;
  36. $scope.map.center.longitude = data.data.Long;
  37.  
  38. }, function () {
  39. alert('Error');
  40. });
  41. }
  42.  
  43. //Show / Hide marker on map
  44. $scope.windowOptions = {
  45. show: true
  46. };
  47.  
  48. });

Step-10: Add view for the action (here "Index") & design.

Right Click on Action Method (here right click on Index action) > Add View... > Enter View Name > Select View Engine (Razor) > Add. HTML Code 
  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4.  
  5. <h2>Index</h2>
  6. <div ng-app="myApp" ng-controller="mapController">
  7. <div class="locations">
  8. <ul>
  9. <li ng-repeat="l in locations" ng-click="ShowLocation(l.LocationID)">{{l.Title}}</li>
  10. </ul>
  11. </div>
  12. <div class="maps">
  13. <!-- Add directive code (gmap directive) for show map and markers-->
  14. <ui-gmap-google-map center="map.center" zoom="map.zoom">
  15. <ui-gmap-marker ng-repeat="marker in markers" coords="marker.coords" options="marker.options" events="marker.events" idkey="marker.id">
  16. <ui-gmap-window options="windowOptions" show="windowOptions.show">
  17. <div style="max-width:200px">
  18. <div class="header"><strong>{{marker.title}}</strong></div>
  19. <div id="mapcontent">
  20. <p>
  21. <img ng-src="{{marker.image}}" style="width:200px; height:100px" />
  22. <div>{{marker.address}}</div>
  23. </p>
  24. </div>
  25. </div>
  26. </ui-gmap-window>
  27. </ui-gmap-marker>
  28. </ui-gmap-google-map>
  29. </div>
  30. </div>
  31.  
  32. @* Now here we need to some css and js *@
  33. <style>
  34. .angular-google-map-container {
  35. height:300px;
  36. }
  37. .angular-google-map {
  38. width:80%;
  39. height:100%;
  40. margin:auto 0px;
  41. }
  42. .locations {
  43. width: 200px;
  44. float: left;
  45. }
  46. .locations ul {
  47. padding: 0px;
  48. margin: 0px;
  49. margin-right: 20px;
  50. }
  51. .locations ul li {
  52. list-style-type: none;
  53. padding: 5px;
  54. border-bottom: 1px solid #f3f3f3;
  55. cursor: pointer;
  56. }
  57. </style>
  58.  
  59. @section Scripts{
  60. @* AngularJS library *@
  61. <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.js"></script>
  62.  
  63. @* google map directive js *@
  64. <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
  65. <script src="//rawgit.com/angular-ui/angular-google-maps/2.0.X/dist/angular-google-maps.js"></script>
  66.  
  67. @* here We will add our created js file *@
  68. <script src="~/Scripts/ngMap.js"></script>
  69. }
  70.  

Step-11: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...