Introduction

In this article, we will learn working with MVC using Angular Maps with two types of maps, which are given below.

  • Google Maps
  • Vector Map

Follow the steps given below to use the Angular map in MVC

  • Create MVC Project
  • Configure the Google /Vector Map
  • Work with Maps

Create MVC Project

Open Visual Studio 2015.

MVC

Go to New menu, click New >> Project. Now, it will open New Project window.

MVC

Select ASP.NET Web Application on Framework 4.6. Enter the name of project in “Solution name” textbox and click OK button.

MVC

One more window should be appearing. Select MVC Template in this popup & click OK button.

Configure the Google Maps

We will download the google ui-map from

Open the _Layout.cshtml and must refer ui-map.min.js file in this View page

  1. “~/Plugin/angular/angular.min.js”>
  2. “~/Plugin/angular-ui-router/release/angular-ui-router.min.js”>
  3. “~/Plugin/angular-ui-map/ui-map.min.js”>
  4. “~/Plugin/angular-ui-event/dist/event.min.js”>”text/javascript”src=https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=onGoogleReady”>

Open the HTML page, then make its design using map attribute.

HTML Code

  1. <section id=“map”>
  2. class=“row”>
  3. class=“col-md-10”>
  4. “marker in myMarkers” ui-map-marker=“myMarkers[$index]” ui-event=“{‘map-click’: ‘openMarkerInfo(marker)’}”>
  5. “myInfoWindow”>
  6. Marker

    Lat:

  7.                     “currentMarkerLat” />, Lng:
  8.                     “currentMarkerLng” />“setMarkerPosition(currentMarker, currentMarkerLat, currentMarkerLng)”>Set Position
  • “map_canvas” ui-map=“myMap” ui-event=“{‘map-click’: ‘addMarker($event, $params)’, ‘map-zoom_changed’: ‘setZoomMessage(myMap.getZoom())’ }” ui-options=“mapOptions” class=“gmap”>
  •             </div>
  • class=“col-md-2”>
  • if=“gmap.myMarkers.length”>Markers

  • “marker in gmap.myMarkers”>
  •                         “” ng-click=“gmap.myMap.panTo(marker.getPosition())”>Marker {{$index}}
  •         </div>
  •     </section>

We have pointed the Map marker directive at an existing google.maps.Marker object, so that it can hook up events –

  • ui-map-marker
  • ui-event

JavaScript Code

Angular Module

Add the ui-map dependency to the Angular module.

  1. var uiroute = angular .module(‘uiroute’, [‘ui.router’“ui.map”]);

Angular Controller

Set the values for attribute. Open the “angular controller” files and hard code an input or you may get & bind the values.

  1. function MapController($timeout) {
  2.         $scope.myMarkers = [];
  3.         $scope.mapOptions = {
  4.             center: new google.maps.LatLng(35.784, -78.670),
  5.             zoom: 15,
  6.             mapTypeId: google.maps.MapTypeId.ROADMAP
  7.         };
  8.         $scope.addMarker = function ($event, $params) {
  9.             $scope.myMarkers.push(new google.maps.Marker({
  10.                 map: $scope.myMap,
  11.                 position: $params[0].latLng
  12.             }));
  13.         };
  14.         $scope.openMarkerInfo = function (marker) {
  15.             $scope.currentMarker = marker;
  16.             $scope.currentMarkerLat = marker.getPosition().lat();
  17.             $scope.currentMarkerLng = marker.getPosition().lng();
  18.             $scope.myInfoWindow.open($scope.myMap, marker);
  19.         };
  20.         $scope.setMarkerPosition = function (marker, lat, lng) {
  21.             marker.setPosition(new google.maps.LatLng(lat, lng));
  22.         };
  23.         $scope.refreshMap = function () {
  24.             $timeout(function () {
  25.                 google.maps.event.trigger($scope.myMap, ‘resize’);
  26.             }, 100);
  27.         };
  28.     }

Here, Google Maps MapOptions object can be passed through the main directive attribute ui-map.

  1. $scope.mapOptions = {
  2.             center: new google.maps.LatLng(35.784, -78.670),
  3.             zoom: 15,
  4.             mapTypeId: google.maps.MapTypeId.ROADMAP
  5.         };

ui-event allows you to specify custom behavior over user event. You just need to prefix the official event by map- to bind a callback it.

  1. “map_canvas” ui-map=“myMap” ui-event=“{‘map-click’: ‘addMarker($event, $params)’, ‘map-zoom_changed’: ‘setZoomMessage(myMap.getZoom())’ }” ui-options=“mapOptions” class=“gmap”>

After completing the entire configuration, add the reference file in _Layout.CSHTML page.

  1. “~/App/App.module.js”>
  2. “~/App/App.config.js”>
  3. “~/App/MapController.js”>

Run the application. We will see the Google Maps with marker.

Output 1

MVC

Configure the Vector Map

You can get this plug-in from here.

You will find various maps in here

Open the _Layout.cshtml and refer the jQuery link in my application.

  1. “~/Plugin/ika.jvectormap/jquery-jvectormap-1.2.2.min.js”>
  2. <link href=“~/Plugin/ika.jvectormap/jquery-jvectormap-1.2.2.css” rel=“stylesheet” />

This script makes it as directive, so we can easily access in HTML.

Angular Directive

  1. return {
  2.         restrict: ‘EA’,
  3.         scope: {
  4.           mapOptions: ‘=’
  5.         },compile: compile};
  6. function compile(tElement, tAttrs, transclude) {
  7.         return {
  8.           post: function(scope, element) {
  9.             var options     = scope.mapOptions,
  10.                 mapHeight   = options.height || ‘250’;
  11.             element.css(‘height’, mapHeight);
  12.             element.vectorMap(options)   }
  13.         };
  14.       }

Now, set the options for this vector map.

Angular Controller

  1. $scope.mapOptions = {
  2.           height:          400,map: ‘world_mill_en’,
  3.           backgroundColor: ‘transparent’,
  4.           zoomMin:         0,
  5.           zoomMax:         8,
  6.           zoomOnScroll:    false,
  7.           regionStyle: {
  8.             initial: {
  9.               ‘fill’:           ‘red’,
  10.               ‘fill-opacity’:   1,
  11.               ‘stroke’:         ‘none’,
  12.               ‘stroke-width’:   1.5,
  13.               ‘stroke-opacity’: 1
  14.             },
  15.             hover: {
  16.               ‘fill-opacity’: 0.8
  17.             },
  18.             selected: {
  19.               fill: ‘blue’
  20.             },
  21.             selectedHover: {
  22.             }
  23.           },
  24.           focusOn:{ x:0.4, y:0.6, scale: 1},
  25.           markerStyle: {
  26.             initial: {
  27.               fill: ‘yellow’,
  28.               stroke: ‘yellow’
  29.             }
  30.           },
  31.           onRegionLabelShow: function(e, el, code) {
  32.             if ( vm.seriesData && vm.seriesData )
  33.               el.html(el.html() + ': ' + vm.seriesData + ' visitors');
  34.           },
  35.           markers: vm.markersData,
  36.           series: {
  37.               regions: [{
  38.                   values: vm.seriesData,
  39.                   scale: [ 'gray' ],
  40.                   normalizeFunction: 'polynomial'
  41.               }]
  42.           },
  43.         };
  1. $scope.mapOptions = {
  2.           height:          400,map: 'world_mill_en',
  3.           backgroundColor: 'transparent',
  4.           zoomMin:         0,
  5.           zoomMax:         8,
  6.           zoomOnScroll:    false,
  7.           regionStyle: {
  8.             initial: {
  9.               'fill':           'red',
  10.               'fill-opacity':   1,
  11.               'stroke':         'none',
  12.               'stroke-width':   1.5,
  13.               'stroke-opacity': 1
  14.             },
  15.             hover: {
  16.               'fill-opacity': 0.8
  17.             },
  18.             selected: {
  19.               fill: 'blue'
  20.             },
  21.             selectedHover: {
  22.             }
  23.           },
  24.           focusOn:{ x:0.4, y:0.6, scale: 1},
  25.           markerStyle: {
  26.             initial: {
  27.               fill: 'yellow',
  28.               stroke: 'yellow'
  29.             }
  30.           },
  31.           onRegionLabelShow: function(e, el, code) {
  32.             if ( vm.seriesData && vm.seriesData )
  33.               el.html(el.html() + ': ' + vm.seriesData + ' visitors');
  34.           },
  35.           markers: vm.markersData,
  36.           series: {
  37.               regions: [{
  38.                   values: vm.seriesData,
  39.                   scale: [ 'gray' ],
  40.                   normalizeFunction: 'polynomial'
  41.               }]
  42.           },
  43.         };

Series data loading in world map

  1. $scope.seriesData = {
  2.         'AU': 15710,
  3.         'RU': 17312,
  4.         'CN': 123370
  5. };

Markers data loading in map

  1. $scope.markersData = [
  2.         {latLng:[41.90, 12.45],  name:'Vatican City'          },
  3.         {latLng:[43.73, 7.41],   name:'Monaco'                },
  4.         {latLng:[-0.52, 166.93], name:'Nauru'                 }
  5.       ];

HTML Code

Vector map will be exposed by using this directives vector-map.

  1. <vector-map map-options="mapOptions"></vector-map>

Call the directive as element in HTML

  1. class="col-lg-12">
  2. class="panel panel-default">
  3. class="panel-body">
  4. Using series and markers data
  5.                     "mapOptions">
  •             </div>
  •         </div>

Now, we are ready to look the vector world map with markers. So, run the application.

Output 2

MVC

Conclusion

In this article, we have learned MVC using Angular Maps. If you have any queries, please tell me through the comments section.

Happy Coding ….

Advertisements