Almost everywhere web and mobile applications are using map. Use of map is increasing day by day to show the geographic data for a user's convenience. A user can find the locations easily using the map. Representation of location is becoming more common in mobile applications as well as in desktop and web applications. If you are looking forward to using a map in your web application, this articles is for you.

Infragistics is one of the companies that provides JavaScript libraries that are easy to use with the JSON data to render the map in your web application. In this article, I will show how can we configure these JavaScript libraries to represent the data on the map.

What will we cover in this article?

  1. Introduction
  2. Creating Asp.net Core Empty Project
  3. Setup MVC
  4. Types of Maps
    1. Open Street
    2. Binding Collection
    3. Scatter Series
    4. Symbol Series
    5. Proportional Symbol Series
    6. Shapes Series
    7. Area Series
    8. Contour Series
    9. Marker Template
    10. Polyline Series
    11. Bing Map

Introduction

Ignite UI for JavaScript map library provides the way to visualize geographic data to your application. It allows rendering the data sets in the form of JSON and other data sources like .shp, dbf, etc consisting of many geographic locations in shapes of markers, lines, polygons, or even interactive bitmaps. This javascript library includes the ability to overlay multiple map layers with geographic data (including data from providers like Bing and Open Street), it allows you to mark specific geographic locations and display information using custom markers and colors.

Creating ASP.NET Core Empty Project

Let’s start the Visual Studio and follow the below steps -

Go to File -> New -> Project -> Choose “Asp.NET Core Web Application”, Set the name of the project and click OK.

Follow the Figure 1. for more details.

ASP.NET Core
Figure 1.

Select the “Empty Project” and click OK. We are using an empty project to demonstrate how to set up MVC in the empty project.

ASP.NET Core
Figure 2.

Now, the project has been created successfully.

Setup MVC

Let’s set up the MVC in the project. Open the solution explorer and right click on the root of the project and add a folder name “Controllers” as shown in Figure 3.

ASP.NET Core
Figure 3.

Add new controller named “HomeController” inside the Controllers folder which will contain all action methods.

ASP.NET Core
Figure 4.

Select the “MVC Controller - Empty” as shown in Figure 5.

ASP.NET Core
Figure 5.

Set the name for the controller HomeController as shown in Figure 6.

ASP.NET Core
Figure 6.

By default, Visual Studio creates an Index action method for our HomeController but it does not create any View for this action method, so let’s create it.

Right-click on the action method and click “Add View” as shown in Figure 7 and 8.

ASP.NET Core
Figure 7.

ASP.NET Core
Figure 8.

In the empty project template, Visual Studio does not add the MVC. We need to add it manually and also, we have to set the root controller with default action method so when we run the application, it self-invokes the default controller and runs the index action method.

To do that, open the startup.cs file and add the highlighted code as shown in Listing 1.

  1. public class Startup
  2. {
  3. public void ConfigureServices(IServiceCollection services)
  4. {
  5. services.AddMvc();
  6. }
  7. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  8. {
  9. app.UseStaticFiles();
  10. app.UseMvc(routes =>
  11. {
  12. routes.MapRoute(
  13. name: "default",
  14. template: "{controller=Home}/{action=Index}/{id?}");
  15. });
  16. }
  17. }

Listing 1.

Now, we have set up the MVC in the empty project template. Let’s build the project and run it in the web browser to see if everything is configured properly.

Add the data file in JSON format

To add the data file shown in Listing 2, right click on the “wwwroot” and add the “Cities.js” file inside it and add the below JSON formatted data to it. This data we will use ass geographic location on the map.

  1. var data = [
  2. {
  3. "Name": "Warsaw",
  4. "Country": "Poland",
  5. "Latitude": 52.21,
  6. "Longitude": 21
  7. },
  8. {
  9. "Name": "London",
  10. "Country": "England",
  11. "Latitude": 51.50,
  12. "Longitude": 0.12
  13. },
  14. {
  15. "Name": "Berlin",
  16. "Country": "Germany",
  17. "Latitude": 52.50,
  18. "Longitude": 13.33
  19. },
  20. {
  21. "Name": "Moscow",
  22. "Country": "Russia",
  23. "Latitude": 55.75,
  24. "Longitude": 37.51
  25. },
  26. {
  27. "Name": "Sydney",
  28. "Country": "Australia",
  29. "Latitude": -33.83,
  30. "Longitude": 151.2
  31. },
  32. {
  33. "Name": "Tokyo",
  34. "Country": "Japan",
  35. "Latitude": 35.6895,
  36. "Longitude": 139.6917
  37. },
  38. {
  39. "Name": "Seoul",
  40. "Country": "South Korea",
  41. "Latitude": 37.5665,
  42. "Longitude": 126.9780
  43. },
  44. {
  45. "Name": "Delhi",
  46. "Country": "India",
  47. "Latitude": 28.6353,
  48. "Longitude": 77.2250
  49. },
  50. {
  51. "Name": "Mumbai",
  52. "Country": "India",
  53. "Latitude": 19.0177,
  54. "Longitude": 72.8562
  55. },
  56. {
  57. "Name": "Manila",
  58. "Country": "Philippines",
  59. "Latitude": 14.6010,
  60. "Longitude": 120.9762
  61. },
  62. {
  63. "Name": "Shanghai",
  64. "Country": "China",
  65. "Latitude": 31.2244,
  66. "Longitude": 121.4759
  67. },
  68. {
  69. "Name": "Mexico City",
  70. "Country": "Mexico",
  71. "Latitude": 19.4270,
  72. "Longitude": -99.1276
  73. },
  74. {
  75. "Name": "New York",
  76. "Country": "United States",
  77. "Latitude": 40.7561,
  78. "Longitude": -73.9870
  79. },
  80. {
  81. "Name": "Sao Paulo",
  82. "Country": "Brasil",
  83. "Latitude": -23.5489,
  84. "Longitude": -46.6388
  85. },
  86. {
  87. "Name": "Los Angeles",
  88. "Country": "United States",
  89. "Latitude": 34.0522,
  90. "Longitude": -118.2434
  91. },
  92. {
  93. "Name": "Sofia",
  94. "Country": "Bulgaria",
  95. "Latitude": 42.697845,
  96. "Longitude": 23.321925
  97. }
  98. ];

Listing 2.

Add the _Layout.cshtml and _ViewStart.cshtml in the project so we can put all common scripts and CSS in that. See the Figure 9 for more details.

ASP.NET Core
Figure 9.

Add the following Infragistics CSS and Scripts file inside the _Layout.cshtml page.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>@ViewData["Title"] - Ignite-UI-Maps</title>
  7. <!-- Ignite UI Required Combined CSS Files -->
  8. <link href="https://www.igniteui.com/igniteui/css/themes/infragistics/infragistics.theme.css" rel="stylesheet" />
  9. <link href="https://www.igniteui.com/igniteui/css/structure/infragistics.css" rel="stylesheet" />
  10. <script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js"></script>
  11. <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  12. <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
  13. <!-- Ignite UI Required Combined JavaScript Files -->
  14. <script src="http://cdn-na.infragistics.com/igniteui/2018.1/latest/js/infragistics.core.js"></script>
  15. <script src="http://cdn-na.infragistics.com/igniteui/2018.1/latest/js/infragistics.dv.js"></script>
  16. <link href="~/CSS/StyleSheet.css" rel="stylesheet" />
  17. <!-- Used to add markup and provide logging
  18. functionality for the API Explorer and API Viewer UI -->
  19. <script src="https://www.igniteui.com/js/apiviewer.js"></script>
  20. <script id="tooltipTemplate" type="text/x-jquery-tmpl">
  21. <table id="tooltipTable">
  22. <tr><th class="tooltipHeading" colspan="2">${item.Name}, ${item.Country}</th></tr>
  23. <tr>
  24. <td>Latitude:</td>
  25. <td>${item.Latitude}</td>
  26. </tr>
  27. <tr>
  28. <td>Longitude:</td>
  29. <td>${item.Longitude}</td>
  30. </tr>
  31. </table>
  32. </script>
  33. </head>
  34. <body>
  35. <div class="container body-content">
  36. @RenderBody()
  37. <hr />
  38. <footer>
  39. <p>© @DateTime.Now.Year.ToString() - Ignite-UI-Maps</p>
  40. </footer>
  41. </div>
  42. </body>
  43. </html>

Listing 3.

Also, add the following CSS here “/CSS/StyleSheet.css”.

  1. #tooltipTable {
  2. font-family: Verdana, Arial, Helvetica, sans-serif;
  3. width: 100%;
  4. border-collapse: collapse;
  5. }
  6. #tooltipTable td, #tooltipTable th {
  7. font-size: 9px;
  8. border: 1px solid #b51c1c;
  9. padding: 3px 7px 2px 7px;
  10. }
  11. #tooltipTable th {
  12. font-weight: bold;
  13. font-size: 11px;
  14. text-align: left;
  15. padding-top: 5px;
  16. padding-bottom: 4px;
  17. background-color: #b51c1c;
  18. color: #ffffff;
  19. }

Listing 4.

Now, add the “Models” folder and add the “WorldCity.cs” model with the following properties.

  1. public class WorldCity
  2. {
  3. public string Name { get; set; }
  4. public string Country { get; set; }
  5. public double Latitude { get; set; }
  6. public double Longitude { get; set; }
  7. }

Listing 5.

Types of Infragistics Maps

There are many types of maps provided by Ignite UI, as given below.

Open Street

Add the action method inside the HomeController for OpenStreets map and add the following code.

  1. public IActionResult OpenStreets()
  2. {
  3. return View();
  4. }

Listing 6.

Now, right click on the OpenStreets action method and click add View to add a view for this action method. A View for OpenStreets will get added at the following location in the project “\Views\Home\OpenStreets.cshtml”.

Add the following HTML and script inside the “OpenStreet.cshtml” for the rendering the Open Street map type.

  1. <!-- Target element for the map -->
  2. <div id="map"></div>
  3. <script src="~/Cities.js"></script>
  4. <script>
  5. $(function () {
  6. $("#map").igMap({
  7. width: "700px",
  8. height: "500px",
  9. windowRect: { left: 0.1, top: 0.1, height: 0.7, width: 0.7 },
  10. // specifies imagery tiles from OpenStreetMap
  11. backgroundContent: {
  12. type: "openStreet"
  13. },
  14. series: [{
  15. type: "geographicSymbol",
  16. name: "worldCities",
  17. dataSource: data,
  18. latitudeMemberPath: "Latitude",
  19. longitudeMemberPath: "Longitude",
  20. markerType: "automatic",
  21. markerCollisionAvoidance: "fade",
  22. markerOutline: "#b51c1c",
  23. markerBrush: "#b51c1c",
  24. showTooltip: true,
  25. tooltipTemplate: "tooltipTemplate"
  26. }]
  27. });
  28. $("#map").find(".ui-widget-content").append("<span class='copyright-notice'><a href='https://www.openstreetmap.org/copyright'>© OpenStreetMap contributors</a></span>")
  29. });
  30. </script>

Listing 7.

Also, add the ActionLink on the Index.cshtml page to access the Open Streets View.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("OpenStreets", "Home")">
  8. Open Streets
  9. </a>
  10. </li>
  11. </ul>

Listing 8.

Now, run the application in a web browser and open the page.

ASP.NET Core
Figure 10.

Binding Collection

Add the action method inside the HomeController for BindingCollection Map and add the following code.

  1. public IActionResult BindingCollection()
  2. {
  3. // Get the content of the world-cities.js file and extract the array data only
  4. // excluding the opening variable declaration
  5. var jsFileName = Path.Combine(env.WebRootPath, "Cities.js");
  6. var jsFileContent = System.IO.File.ReadAllText(jsFileName);
  7. var openingBracePos = jsFileContent.IndexOf('[');
  8. var closingBracePos = jsFileContent.IndexOf(']');
  9. var jsonContent = jsFileContent.Substring(openingBracePos, closingBracePos - openingBracePos + 1);
  10. // De-serialize WorldCities data from a JavaScript array into List<WorldCity>
  11. var wordlCities = Newtonsoft.Json.JsonConvert.DeserializeObject<List<WorldCity>>(jsonContent);
  12. return View("BindingCollection", wordlCities.AsQueryable());
  13. }

Listing 9.

Now, right click on the BindingCollection action method and click add View to add a view for this action method. A View for BindingCollection will get added at the following location in the project “\Views\Home\BindingCollection.cshtml”.

Add the following HTML and script inside the “BindingCollection.cshtml” for rendering the Binding Collection map type.

  1. @using Infragistics.Web.Mvc;
  2. @model IQueryable<Ignite_UI_Map_Demo.Models.WorldCity>
  3. <div>
  4. @(Html.Infragistics().Map<Ignite_UI_Map_Demo.Models.WorldCity>(Model)
  5. .ID("map")
  6. .Width("700px")
  7. .Height("500px")
  8. .OverviewPlusDetailPaneVisibility(Visibility.Visible)
  9. .OverviewPlusDetailPaneBackgroundImageUri(Url.Content("~/images/samples/maps/World.png"))
  10. .BackgroundContent(bgr => bgr.OpenStreetMaps())
  11. .PanModifier(ModifierKeys.Control)
  12. .Series(series =>
  13. {
  14. series.GeographicSymbol("worldCities")
  15. .LatitudeMemberPath(item => item.Latitude)
  16. .LongitudeMemberPath(item => item.Longitude)
  17. .MarkerType(MarkerType.Automatic)
  18. .MarkerCollisionAvoidance(CollisionAvoidanceType.Fade)
  19. .MarkerBrush("#1B559D")
  20. .MarkerOutline("black")
  21. .ShowTooltip(true).TooltipTemplate("tooltipTemplate");
  22. })
  23. .WindowRect(0.1, 0.1, 0.7, 0.7)
  24. .DataBind()
  25. .Render()
  26. )
  27. </div>
  28. <script type="text/javascript">
  29. $(function () {
  30. $("#map").find(".ui-widget-content")
  31. .append("<span class='copyright-notice'><a href='http://www.openstreetmap.org/copyright'>© OpenStreetMap contributors</a></span>");
  32. });
  33. </script>

Listing 10.

Also, add the ActionLink on the Index.cshtml page to access the Binding Collection view.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("BindingCollection", "Home")">
  8. Binding Collection
  9. </a>
  10. </li>
  11. </ul>

Listing 11.

Now, run the application in the web browser and open the page.

ASP.NET Core
Figure 11.

Scatter Series

Add the action method inside the Home Controller for ScatterSeries map and add the following code.

  1. public IActionResult ScatterSeries()
  2. {
  3. return View();
  4. }

Listing 12.

Now right click on the ScatterSeries action method and click add View to add a view for this action method. A View for ScatterSeries will get added at the following location in the project “\Views\Home\ScatterSeries.cshtml”.

Add the following HTML and script inside the “ScatterSeries.cshtml” for the rendering the Scatter Series map type.

  1. @{
  2. ViewData["Title"] = "ScatterSeries";
  3. }
  4. <style type="text/css">
  5. .sliderBounds {
  6. width: 95px;
  7. margin: 6px 3px 6px 6px;
  8. display: inline-block;
  9. }
  10. .optionContainer {
  11. width: 200px;
  12. margin: 2px;
  13. padding: 2px;
  14. display: inline-block;
  15. border: 1px solid #e0e0e0;
  16. -moz-border-radius: 4px;
  17. -webkit-border-radius: 4px;
  18. border-radius: 4px;
  19. }
  20. .selectBounds {
  21. min-width: 120px;
  22. max-width: 132px;
  23. }
  24. </style>
  25. <h2>Scatter Series</h2>
  26. <script src="https://www.igniteui.com/data-files/high-density-data.js"></script>
  27. <script id="cityTemplate" type="text/x-jquery-tmpl">
  28. <table>
  29. <tr><td class="tooltipHeading" colspan="2">${item.name}</td></tr>
  30. </table>
  31. </script>
  32. <style type="text/css">
  33. .sliderBounds {
  34. width: 95px;
  35. margin: 6px 3px 6px 6px;
  36. display: inline-block;
  37. }
  38. .optionContainer {
  39. width: 200px;
  40. margin: 2px;
  41. padding: 2px;
  42. display: inline-block;
  43. border: 1px solid #e0e0e0;
  44. -moz-border-radius: 4px;
  45. -webkit-border-radius: 4px;
  46. border-radius: 4px;
  47. }
  48. .selectBounds {
  49. min-width: 120px;
  50. max-width: 132px;
  51. }
  52. </style>
  53. <div id="map"></div>
  54. <br />
  55. <div>
  56. <fieldset id="chartOptions">
  57. <legend>Map Options</legend>
  58. <br />
  59. <div class="optionContainer">
  60. Resolution
  61. <br />
  62. <div id="resolutionSlider" class="sliderBounds"></div>
  63. <label id="resolutionLabel">0</label>
  64. </div>
  65. <div class="optionContainer">
  66. Minimum Heat Value
  67. <br />
  68. <div id="minimumHeatValueSlider" class="sliderBounds"></div>
  69. <label id="minimumHeatValueLabel">0</label>
  70. </div>
  71. <div class="optionContainer">
  72. Maximum Heat Value
  73. <br />
  74. <div id="maximumHeatValueSlider" class="sliderBounds"></div>
  75. <label id="maximumHeatValueLabel">5</label>
  76. </div>
  77. <br />
  78. <div class="optionContainer">
  79. Point Extent
  80. <br />
  81. <div id="pointExtentSlider" class="sliderBounds"></div>
  82. <label id="pointExtentLabel">1</label>
  83. </div>
  84. <div class="optionContainer">
  85. Minimum Heat Color
  86. <br />
  87. <select id="minimumHeatColorDropDown" class="selectBounds">
  88. <option value="red">Red</option>
  89. <option value="blue">Blue</option>
  90. <option value="green">Green</option>
  91. <option value="yellow">Yellow</option>
  92. <option value="black" selected="selected">Black</option>
  93. </select>
  94. </div>
  95. <div class="optionContainer">
  96. Maximum Heat Color
  97. <br />
  98. <select id="maximumHeatColorDropDown" class="selectBounds">
  99. <option value="red" selected="selected">Red</option>
  100. <option value="blue">Blue</option>
  101. <option value="green">Green</option>
  102. <option value="yellow">Yellow</option>
  103. <option value="black">Black</option>
  104. </select>
  105. </div>
  106. <br />
  107. <div class="optionContainer">
  108. <input type="checkbox" id="enableMouseOverCheckBox" checked="checked" />Enable Mouse Over<br />
  109. </div>
  110. <div class="optionContainer">
  111. <input type="checkbox" id="useBruteForceCheckBox" />Use Brute Force<br />
  112. </div>
  113. </fieldset>
  114. </div>
  115. <br />
  116. <script type="text/javascript">
  117. $(function () {
  118. $("#map").igMap({
  119. width: "700px",
  120. height: "500px",
  121. backgroundContent: {
  122. type: "openStreet"
  123. },
  124. series: [{
  125. type: "geographicHighDensityScatter",
  126. name: "australiaMap",
  127. dataSource: placeData,
  128. latitudeMemberPath: "lat",
  129. longitudeMemberPath: "lon",
  130. heatMinimum: 0,
  131. heatMaximum: 5,
  132. mouseOverEnabled: true,
  133. showTooltip: true,
  134. tooltipTemplate: "cityTemplate"
  135. }],
  136. windowRect: {
  137. left: 0.7,
  138. top: 0.52,
  139. width: 0.127,
  140. height: 0.127
  141. }
  142. });
  143. $("#map").find(".ui-widget-content").append("<span class='copyright-notice'><a href='https://www.openstreetmap.org/copyright'>© OpenStreetMap contributors</a></span>");
  144. // Resolution
  145. $("#resolutionSlider").slider({
  146. min: 0,
  147. max: 10,
  148. value: 0,
  149. slide: function (event, ui) {
  150. $("#map").igMap("option", "series", [{ name: "australiaMap", resolution: ui.value }]);
  151. $("#resolutionLabel").text(ui.value);
  152. }
  153. });
  154. // Minimum Heat Value
  155. $("#minimumHeatValueSlider").slider({
  156. min: 0,
  157. max: 5,
  158. value: 0,
  159. slide: function (event, ui) {
  160. $("#map").igMap("option", "series", [{ name: "australiaMap", heatMinimum: ui.value }]);
  161. $("#minimumHeatValueLabel").text(ui.value);
  162. }
  163. });
  164. // Maximum Heat Value
  165. $("#maximumHeatValueSlider").slider({
  166. min: 5,
  167. max: 10,
  168. value: 10,
  169. slide: function (event, ui) {
  170. $("#map").igMap("option", "series", [{ name: "australiaMap", heatMaximum: ui.value }]);
  171. $("#maximumHeatValueLabel").text(ui.value);
  172. }
  173. });
  174. // Heat Minimum Color
  175. $("#minimumHeatColorDropDown").on({
  176. change: function (e) {
  177. var minColor = $(this).val();
  178. $("#map").igMap("option", "series", [{ name: "australiaMap", heatMinimumColor: minColor }]);
  179. }
  180. });
  181. // Heat Maximum Color
  182. $("#maximumHeatColorDropDown").on({
  183. change: function (e) {
  184. var maxColor = $(this).val();
  185. $("#map").igMap("option", "series", [{ name: "australiaMap", heatMaximumColor: maxColor }]);
  186. }
  187. });
  188. // Point Extent
  189. $("#pointExtentSlider").slider({
  190. min: 1,
  191. max: 5,
  192. value: 1,
  193. slide: function (event, ui) {
  194. $("#map").igMap("option", "series", [{ name: "australiaMap", pointExtent: ui.value }]);
  195. $("#pointExtentLabel").text(ui.value);
  196. }
  197. });
  198. // Enable Mouse Over
  199. $("#enableMouseOverCheckBox").click(function (e) {
  200. var enableMouseOverSeries = $("#enableMouseOverCheckBox").is(":checked") ? true : false;
  201. $("#map").igMap("option", "series", [{ name: "australiaMap", mouseOverEnabled: enableMouseOverSeries }]);
  202. });
  203. // Use Brute Force
  204. $("#useBruteForceCheckBox").click(function (e) {
  205. var useBruteForceSeries = $("#useBruteForceCheckBox").is(":checked") ? true : false;
  206. $("#map").igMap("option", "series", [{ name: "australiaMap", useBruteForce: useBruteForceSeries }]);
  207. });
  208. });
  209. </script>

Listing 13.

Also, add the ActionLink on the Index.cshtml page to access the Scatter Series view.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("ScatterSeries", "Home")">
  8. Scatter Series
  9. </a>
  10. </li>
  11. </ul>

Listing 14.

Now, run the application in the web browser and open the page.

ASP.NET Core
Figure 12.

Symbol Series

Add the action method inside the HomeController for SymbolSeries Map and add the following code.

  1. public IActionResult SymbolSeries()
  2. {
  3. return View();
  4. }

Listing 15.

Now, right click on the SymbolSeries action method and click "Add View" to add a view for this action method. A View for SymbolSeries will get added at the following location in the project “\Views\Home\SymbolSeries.cshtml”.

Add the following HTML and script inside the “SymbolSeries.cshtml” for the rendering the Symbol Series map type.

  1. @{
  2. ViewData["Title"] = "SymbolSeries";
  3. }
  4. <script src="https://www.igniteui.com/js/map-helper.js" type="text/javascript"></script>
  5. <style>
  6. #tooltipTable {
  7. font-family: Verdana, Arial, Helvetica, sans-serif;
  8. width: 100%;
  9. border-collapse: collapse;
  10. }
  11. #tooltipTable td, #tooltipTable th {
  12. font-size: 9px;
  13. border: 1px solid #5da7df;
  14. padding: 3px 7px 2px 7px;
  15. }
  16. #tooltipTable th {
  17. font-weight: bold;
  18. font-size: 11px;
  19. text-align: left;
  20. padding-top: 5px;
  21. padding-bottom: 4px;
  22. background-color: #5da7df;
  23. color: #ffffff;
  24. }
  25. </style>
  26. <script id="geoSymbolTooltip" type="text/x-jquery-tmpl">
  27. <table id="tooltipTable">
  28. <tr><th colspan="2">${item.City}, ${item.Country}</th></tr>
  29. <tr>
  30. <td>Population:</td>
  31. <td>${item.Population}</td>
  32. </tr>
  33. </table>
  34. </script>
  35. <h2>Symbol Series</h2>
  36. <script type="text/javascript">
  37. $(function () {
  38. // load geo-locations from shapefile and simplify data structure
  39. var worldCities = [];
  40. var points = [];
  41. var shapeDataSource = new $.ig.ShapeDataSource({
  42. shapefileSource: "https://www.igniteui.com/data-files/shapes/world_cities.shp",
  43. databaseSource: "https://www.igniteui.com/data-files/shapes/world_cities.dbf",
  44. transformRecord: function (rec) {
  45. var pointX = rec.points.item(0).item(0).x();
  46. var pointY = rec.points.item(0).item(0).y();
  47. var city = rec.fields.item("NAME");
  48. var country = rec.fields.item("COUNTRY");
  49. var population = rec.fields.item("POPULATION");
  50. if (population > 7000) {
  51. points.push({
  52. Latitude: pointY, Longitude: pointX,
  53. City: city, Country: country,
  54. Population: population
  55. });
  56. }
  57. },
  58. callback: function () {
  59. worldCities = points.sort(compareCities);;
  60. createMap();
  61. }
  62. });
  63. shapeDataSource.dataBind();
  64. function compareCities(a, b) {
  65. if (a.Population > b.Population)
  66. return -1;
  67. if (a.Population < b.Population)
  68. return 1;
  69. return 0;
  70. }
  71. function createMap() {
  72. $("#map").igMap({
  73. width: "700px",
  74. height: "500px",
  75. windowRect: { left: 0.1, top: 0.1, height: 0.7, width: 0.7 },
  76. overviewPlusDetailPaneVisibility: "visible",
  77. overviewPlusDetailPaneBackgroundImageUri: "https://www.igniteui.com/images/samples/maps/world.png",
  78. backgroundContent: {
  79. type: "openStreet"
  80. },
  81. series: [{
  82. type: "geographicSymbol",
  83. name: "worldCities",
  84. dataSource: worldCities,
  85. longitudeMemberPath: "Longitude",
  86. latitudeMemberPath: "Latitude",
  87. showTooltip: true,
  88. tooltipTemplate: "geoSymbolTooltip",
  89. // using custom template
  90. markerType: "none",
  91. markerTemplate: {
  92. measure: function (measureInfo) {
  93. measureInfo.width = 10;
  94. measureInfo.height = 10;
  95. },
  96. render: function (renderInfo) {
  97. var ctx = renderInfo.context;
  98. var x = renderInfo.xPosition;
  99. var y = renderInfo.yPosition;
  100. if (renderInfo.isHitTestRender)
  101. ctx.fillStyle = renderInfo.data.actualItemBrush().fill();
  102. else
  103. ctx.fillStyle = "black";
  104. var size = 10;
  105. var heightHalf = size / 2.0;
  106. var widthHalf = size / 2.0;
  107. if (renderInfo.isHitTestRender) {
  108. ctx.fillRect(x - widthHalf, y - heightHalf, size, size);
  109. } else {
  110. // color markers based on population of cities
  111. var pop = renderInfo.data.item()["Population"];
  112. if (pop > 0)
  113. ctx.fillStyle = "rgba(120,120,120,0.8)";
  114. if (pop > 100000)
  115. ctx.fillStyle = "rgba(160,200,20,0.8)";
  116. if (pop > 1000000)
  117. ctx.fillStyle = "rgba(210,155,20,0.8)";
  118. if (pop > 5000000)
  119. ctx.fillStyle = "rgba(210,60,20,0.8)";
  120. if (pop > 10000000)
  121. ctx.fillStyle = "rgba(155,20,20,0.8)";
  122. size = 3;
  123. ctx.globalAlpha = 1;
  124. ctx.strokeStyle = "rgba(20,20,20,0.36)";
  125. ctx.beginPath();
  126. ctx.arc(x, y, size, 0, 2.0 * Math.PI);
  127. ctx.fill();
  128. ctx.stroke();
  129. }
  130. }
  131. }
  132. }]
  133. });
  134. $("#map").find(".ui-widget-content").append("<span class='copyright-notice'><a href='https://www.openstreetmap.org/copyright'>© OpenStreetMap contributors</a></span>");
  135. };
  136. });
  137. </script>
  138. <div>
  139. <div id="map"></div>
  140. </div>

Listing 16.

Also, add the ActionLink on the Index.cshtml page to access the Symbol Series view.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("SymbolSeries", "Home")">
  8. Symbol Series
  9. </a>
  10. </li>
  11. </ul>

Listing 17.

Now, run the application in the web browser and open the page.

ASP.NET Core
Figure 13.

Proportional Symbol Series

Add the action method in side the HomeController for ProportionalSymbolSeries map and add the following code,

  1. public IActionResult ProportionalSymbolSeries()
  2. {
  3. return View();
  4. }

Listing 18.

Now right click on the ProprotionalSymbolSeries action method and click "Add View" to add a view for this action method. A view for ProprotionalSymbolSeries will get added at the following location in the project “\Views\Home\ProprotionalSymbolSeries.cshtml”.

Add the following HTML and script inside the “ProprotionalSymbolSeries.cshtml” for the rendering the Proportional Symbol Series map type.

  1. @{
  2. ViewData["Title"] = "ProportionalSymbolSeries";
  3. }
  4. <script id="geoSymbolTooltip" type="text/x-jquery-tmpl">
  5. <table id="tooltipTable">
  6. <tr><th colspan="2">${item.City}, ${item.Country}</th></tr>
  7. <tr>
  8. <td>Population:</td>
  9. <td>${item.Population}</td>
  10. </tr>
  11. </table>
  12. </script>
  13. <h2>Proportional Symbol Series</h2>
  14. <script type="text/javascript">
  15. $(function () {
  16. function CreateEarthquakes(count) {
  17. count = count || 50;
  18. var data = [];
  19. for (i = 0; i < count; i++)
  20. {
  21. var x = Math.floor((Math.random() * 360) - 180);
  22. var y = Math.floor((Math.random() * 180) - 90);
  23. var m = Math.floor((Math.random() * 10) + 1);
  24. var point = { "Magnitude": m, "Longitude": x, "Latitude": y };
  25. data.push(point);
  26. }
  27. return data;
  28. }
  29. var data = CreateEarthquakes(100);
  30. function createMap() {
  31. $("#map").igMap({
  32. width: "700px",
  33. height: "500px",
  34. series: [{
  35. name: "series1",
  36. type: "geographicProportionalSymbol",
  37. dataSource: data,
  38. markerType: "circle",
  39. markerOutline: "black",
  40. longitudeMemberPath: "Longitude",
  41. latitudeMemberPath: "Latitude",
  42. radiusMemberPath: "Magnitude",
  43. radiusScale: {
  44. minimumValue: 10,
  45. maximumValue: 40,
  46. },
  47. fillMemberPath: "Magnitude",
  48. fillScale: {
  49. type: "value",
  50. brushes: ["red", "yellow"],
  51. minimumValue: 1,
  52. maximumValue: 12,
  53. }
  54. },
  55. ]
  56. });
  57. };
  58. createMap();
  59. });
  60. </script>
  61. <div>
  62. <div id="map"></div>
  63. </div>

Listing 19.

Also, add the ActionLink on the Index.cshtml page to access the Symbol Series view.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("ProportionalSymbolSeries", "Home")">
  8. Proportional Symbol Series
  9. </a>
  10. </li>
  11. </ul>

Listing 20.

Now, run the application in a web browser and open the page.

ASP.NET Core
Figure 14.

Shapes Series

Add the action method inside the HomeController for ShapesSeries Map and add the following code.

  1. public IActionResult ShapesSeries()
  2. {
  3. return View();
  4. }

Listing 21.

Now right click on the ShapesSeries action method and click add View to add a view for this action method. A View for ProprotionalSymbolSeries will get added at the following location in the project “\Views\Home\ShapesSeries.cshtml”.

Add the following HTML and script inside the “ShapesSeries.cshtml” for the rendering the Shapes Series map type.

  1. @{
  2. ViewData["Title"] = "ShapesSeries";
  3. }
  4. <h2>Shapes Series</h2>
  5. <style>
  6. #tooltipTable {
  7. font-family: Verdana, Arial, Helvetica, sans-serif;
  8. width: 100%;
  9. border-collapse: collapse;
  10. }
  11. #tooltipTable td, #tooltipTable th {
  12. font-size: 9px;
  13. border: 1px solid #2d87cb;
  14. padding: 3px 7px 2px 7px;
  15. }
  16. #tooltipTable th {
  17. font-weight: bold;
  18. font-size: 11px;
  19. text-align: left;
  20. padding-top: 5px;
  21. padding-bottom: 4px;
  22. background-color: #2d87cb;
  23. color: #ffffff;
  24. }
  25. </style>
  26. <script id="geoShapeTooltip" type="text/x-jquery-tmpl">
  27. <table id="tooltipTable">
  28. <tr>
  29. <th class="tooltipHeading" colspan="2">
  30. ${item.fieldValues.NAME}, ${item.fieldValues.REGION}
  31. </th>
  32. </tr>
  33. <tr>
  34. <td>Population:</td>
  35. <td>${item.fieldValues.POP2005}</td>
  36. </tr>
  37. </table>
  38. </script>
  39. <script type="text/javascript" src="https://www.igniteui.com/js/map-helper.js"></script>
  40. <script type="text/javascript">
  41. function ColorPickerByIndex(min, max) {
  42. // Initialize internal state
  43. var brushes = ["#d9c616", "#d96f17", "#d1150c"];
  44. var interval = (max - min) / (brushes.length - 1);
  45. // Returns a color from the brushes array depending on the input value
  46. this.getColorByIndex = function(val) {
  47. var index = Math.round(val / interval);
  48. if (index < 0) {
  49. index = 0;
  50. } else if (index > (brushes.length - 1)) {
  51. index = brushes.length - 1;
  52. }
  53. return brushes[index];
  54. };
  55. }
  56. $(function () {
  57. var colorPicker = new ColorPickerByIndex(100000, 500000000);
  58. // alternative functions for generating color based on a data value
  59. var getColorValue = function (val) {
  60. var ratio = val / 1338299500.0;
  61. var col = 255.0 * ratio;
  62. var colString = "rgba(0,50," + Math.round(col) + ",0.45)";
  63. return colString;
  64. };
  65. var getLogColorValue = function (val) {
  66. var ratio = Math.log(val) / Math.log(1338299500.0);
  67. var col = 255.0 * ratio;
  68. var colString = "rgba(0,50," + Math.round(col) + ",0.45)";
  69. return colString;
  70. };
  71. $("#map").igMap({
  72. width: "700px",
  73. height: "500px",
  74. windowRect: { left: 0.1, top: 0.1, height: 0.7, width: 0.7 },
  75. overviewPlusDetailPaneVisibility: "visible",
  76. overviewPlusDetailPaneBackgroundImageUri: "https://www.igniteui.com/images/samples/maps/world.png",
  77. series: [{
  78. type: "geographicShape",
  79. name: "worldCountries",
  80. markerType: "none",
  81. shapeMemberPath: "points",
  82. shapeDataSource: 'https://www.igniteui.com/data-files/shapes/world_countries_reg.shp',
  83. databaseSource: 'https://www.igniteui.com/data-files/shapes/world_countries_reg.dbf',
  84. opacity: 0.8,
  85. outlineThickness: 1,
  86. showTooltip: true,
  87. tooltipTemplate: "geoShapeTooltip",
  88. shapeStyleSelector: {
  89. selectStyle: function (s, o) {
  90. var pop = s.fields.item("POP2005");
  91. var popInt = parseInt(pop);
  92. var colString = colorPicker.getColorByIndex(popInt); / /getColorValue(popInt);
  93. return {
  94. fill: colString,
  95. stroke: "gray"
  96. };
  97. }
  98. }
  99. }]
  100. });
  101. $("#map").find(".ui-widget-content").append("<span class='copyright-notice'><a href='https://www.openstreetmap.org/copyright'>© OpenStreetMap contributors</a></span>");
  102. });
  103. </script>
  104. <div id="map"></div>

Listing 22.

Also, add the ActionLink on the Index.cshtml page to access the Symbol Series view.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("ShapesSeries", "Home")">
  8. Shapes Series
  9. </a>
  10. </li>
  11. </ul>

Listing 23.

Now, run the application in the web browser and open the page.

ASP.NET Core
Figure 15.

Area Series

Add the action method inside the HomeController for AreaSeries map and add the following code.

  1. public IActionResult AreaSeries()
  2. {
  3. return View();
  4. }

Listing 21.

Now right click on the AreaSeries action method and click add View to add a view for this action method. A View for AreaSeries will get added at the following location in the project “\Views\Home\AreaSeries.cshtml”.

Add the following HTML and script inside the “AreaSeries.cshtml” for the rendering the Area Series map type.

  1. @{
  2. ViewData["Title"] = "Area Series";
  3. }
  4. <h2>Area Series</h2>
  5. <script type="text/javascript">
  6. $(function () {
  7. $("#map").igMap({
  8. width: "700px",
  9. height: "500px",
  10. overviewPlusDetailPaneVisibility: "visible",
  11. overviewPlusDetailPaneBackgroundImageUri: "https://www.igniteui.com/images/samples/maps/world.png",
  12. series: [{
  13. type: "geographicScatterArea",
  14. name: "precipitation",
  15. colorScale: {
  16. type: "customPalette",
  17. interpolationMode: "interpolateRGB",
  18. minimumValue: 0.15,
  19. palette: [
  20. "#3300CC", "#4775FF", "#0099CC", "#00CC99",
  21. "#33CC00", "#99CC00", "#CC9900", "#FFC20A", "#CC3300"]
  22. },
  23. triangleVertexMemberPath1: "v1",
  24. triangleVertexMemberPath2: "v2",
  25. triangleVertexMemberPath3: "v3",
  26. longitudeMemberPath: "pointX",
  27. latitudeMemberPath: "pointY",
  28. colorMemberPath: "value",
  29. triangulationDataSource: "https://www.igniteui.com/data-files/shapes/nws_precip_2011091820.itf",
  30. }],
  31. windowRect: {
  32. left: 0.31,
  33. top: 0.375,
  34. height: 0.025,
  35. width: 0.025
  36. }
  37. });
  38. $("#map").find(".ui-widget-content").append("<span class='copyright-notice'><a href='https://www.openstreetmap.org/copyright'>© OpenStreetMap contributors</a></span>");
  39. });
  40. </script>
  41. <div id="map"></div>

Listing 22.

Also, add the ActionLink on the Index.cshtml page to access the Symbol Series view.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("AreaSeries", "Home")">
  8. Area Series
  9. </a>
  10. </li>
  11. </ul>

Listing 23.

Now, run the application in a web browser and open the page.

ASP.NET Core
Figure 16.

Contour Series

Add the action method inside the HomeController for ContourSeries map and add the following code.

  1. public IActionResult ContourSeries()
  2. {
  3. return View();
  4. }

Listing 21.

Now, right click on the ContourSeries action method and click add View to add a view for this action method. A view for ContourSeries will get added at the following location in the project “\Views\Home\ContourSeries.cshtml”.

Add the following HTML and script inside the “ContourSeries.cshtml” for rendering the Contour Series map type.

  1. @{
  2. ViewData["Title"] = "Contour Series";
  3. }
  4. <h2>Contour Series</h2>
  5. <script type="text/javascript">
  6. $(function () {
  7. $("#map").igMap({
  8. width: "700px",
  9. height: "500px",
  10. overviewPlusDetailPaneVisibility: "visible",
  11. overviewPlusDetailPaneBackgroundImageUri: "https://www.igniteui.com/images/samples/maps/world.png",
  12. series: [{
  13. type: "geographicContourLine",
  14. name: "precipitation",
  15. fillScale: {
  16. type: "value",
  17. brushes: [
  18. "#3300CC", "#4775FF", "#0099CC", "#00CC99",
  19. "#33CC00", "#99CC00", "#CC9900", "#FFC20A", "#CC3300"]
  20. },
  21. triangleVertexMemberPath1: "v1",
  22. triangleVertexMemberPath2: "v2",
  23. triangleVertexMemberPath3: "v3",
  24. longitudeMemberPath: "pointX",
  25. latitudeMemberPath: "pointY",
  26. valueMemberPath: "value",
  27. triangulationDataSource: "https://www.igniteui.com/data-files/shapes/nws_precip_2011091820.itf",
  28. }],
  29. windowRect: {
  30. left: 0.31,
  31. top: 0.375,
  32. height: 0.025,
  33. width: 0.025
  34. }
  35. });
  36. $("#map").find(".ui-widget-content").append("<span class='copyright-notice'><a href='https://www.openstreetmap.org/copyright'>© OpenStreetMap contributors</a></span>");
  37. });
  38. </script>
  39. <div id="map"></div>

Listing 22.

Also, add the ActionLink on the Index.cshtml page to access the Countor Series view.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("AreaSeries", "Home")">
  8. Area Series
  9. </a>
  10. </li>
  11. </ul>

Listing 23.

Now run the application in web browser and open the page.

Marker Template

Add the action method in side the Home Controller for MarkerTemplate map and add the following code.

  1. public IActionResult MarkerTemplate()
  2. {
  3. return View();
  4. }

Listing 24.

Now right click on the MarkerTemplate action method and click add View to add a view for this action method. A View for MarkerTemplate will get added at the following location in the project “\Views\Home\MarkerTemplate.cshtml”

Add the following html and script in side the “MarkerTemplate.cshtml” for the rendering the market template map type.

  1. @{
  2. ViewData["Title"] = "Marker Template";
  3. }
  4. <style>
  5. #customTable2 {
  6. font-family: Verdana, Arial, Helvetica, sans-serif;
  7. width: 100%;
  8. border-collapse: collapse;
  9. }
  10. #customTable2 td, #customTable2 th {
  11. font-size: 9px;
  12. border: 1px solid #2d87cb;
  13. padding: 3px 7px 2px 7px;
  14. }
  15. #customTable2 th {
  16. font-weight: bold;
  17. font-size: 11px;
  18. text-align: left;
  19. padding-top: 5px;
  20. padding-bottom: 4px;
  21. background-color: #2d87cb;
  22. color: #ffffff;
  23. }
  24. </style>
  25. <script id="customTooltip" type="text/x-jquery-tmpl">
  26. <table id="customTable2">
  27. <tr><th colspan="2">${item.Name}</th></tr>
  28. <tr>
  29. <td>Office Type:</td>
  30. <td>${item.Type}</td>
  31. </tr>
  32. </table>
  33. </script>
  34. <h2>Marker Template</h2>
  35. <script src="https://www.igniteui.com/data-files/ig-offices.js"></script>
  36. <script type="text/javascript">
  37. $(function () {
  38. $("#map").igMap({
  39. width: "700px",
  40. height: "500px",
  41. overviewPlusDetailPaneVisibility: "visible",
  42. overviewPlusDetailPaneBackgroundImageUri: "https://www.igniteui.com/images/samples/maps/world.png",
  43. windowRect: { left: 0.4, top: 0.2, height: 0.6, width: 0.6 },
  44. backgroundContent: {
  45. type: "openStreet"
  46. },
  47. series: [
  48. {
  49. type: "geographicSymbol",
  50. name: "igOffices",
  51. dataSource: igOffices,
  52. latitudeMemberPath: "Latitude",
  53. longitudeMemberPath: "Longitude",
  54. showTooltip: true,
  55. tooltipTemplate: "customTooltip",
  56. markerCollisionAvoidance: "fade",
  57. // Defines marker template rendering function
  58. markerTemplate: {
  59. measure: function (measureInfo) {
  60. measureInfo.width = 10;
  61. measureInfo.height = 10;
  62. },
  63. render: function (renderInfo) {
  64. createMarker(renderInfo);
  65. }
  66. }
  67. }
  68. ]
  69. });
  70. $("#map").find(".ui-widget-content").append("<span class='copyright-notice'><a href='https://www.openstreetmap.org/copyright'>© OpenStreetMap contributors</a></span>");
  71. });
  72. function createMarker(renderInfo) {
  73. var ctx = renderInfo.context;
  74. var x = renderInfo.xPosition;
  75. var y = renderInfo.yPosition;
  76. var size = 10;
  77. var heightHalf = size / 2.0;
  78. var widthHalf = size / 2.0;
  79. if (renderInfo.isHitTestRender) {
  80. // This is called for tooltip hit test only
  81. // Rough marker rectangle size calculation
  82. ctx.fillStyle = renderInfo.data.actualItemBrush().fill();
  83. ctx.fillRect(x - widthHalf, y - heightHalf, size, size);
  84. }
  85. else
  86. {
  87. var data = renderInfo.data;
  88. var name = data.item()["Name"];
  89. var type = data.item()["ID"];
  90. // Draw text
  91. ctx.textBaseline = "top";
  92. ctx.font = '8pt Verdana';
  93. ctx.fillStyle = "black";
  94. ctx.textBaseline = "middle";
  95. wrapText(ctx, name, x + 3, y + 6, 80, 12);
  96. // Draw marker
  97. ctx.beginPath();
  98. ctx.arc(x, y, 4, 0, 2 * Math.PI, false);
  99. if (type == "Marketing")
  100. ctx.fillStyle = "#2372D1";
  101. else if (type == "Support")
  102. ctx.fillStyle = "#4d4949";
  103. else if (type == "Development Lab")
  104. ctx.fillStyle = "#d13521";
  105. else
  106. ctx.fillStyle = "#36a815";
  107. ctx.fill();
  108. ctx.lineWidth = 1;
  109. ctx.strokeStyle = "black";
  110. ctx.stroke();
  111. }
  112. }
  113. // Plots a rectangle with rounded corners with a semi-transparent frame
  114. function plotTextBackground(context, left, top, width, height) {
  115. var cornerRadius = 3;
  116. context.beginPath();
  117. // Upper side and upper right corner
  118. context.moveTo(left + cornerRadius, top);
  119. context.lineTo(left + width - cornerRadius, top);
  120. context.arcTo(left + width, top, left + width, top + cornerRadius, cornerRadius);
  121. // Right side and lower right corner
  122. context.lineTo(left + width, top + height - cornerRadius);
  123. context.arcTo(left + width, top + height, left + width - cornerRadius, top + height, cornerRadius);
  124. // Lower side and lower left corner
  125. context.lineTo(left + cornerRadius, top + height);
  126. context.arcTo(left, top + height, left, top + height - cornerRadius, cornerRadius);
  127. // Left side and upper left corner
  128. context.lineTo(left, top + cornerRadius);
  129. context.arcTo(left, top, left + cornerRadius, top, cornerRadius);
  130. // Fill white with 75% opacity
  131. context.globalAlpha = 1;
  132. context.fillStyle = "white";
  133. context.fill();
  134. context.globalAlpha = 1;
  135. // Plot grey frame
  136. context.lineWidth = 1;
  137. context.strokeStyle = "grey";
  138. context.stroke();
  139. }
  140. // Outputs text in a word wrapped fashion in a transparent frame
  141. function wrapText(context, text, x, y, maxWidth, lineHeight) {
  142. var words = text.split(" ");
  143. var line = "";
  144. var yCurrent = y;
  145. var lines = [], currentLine = 0;
  146. // Find the longest word in the text and update the max width if the longest word cannot fit
  147. for (var i = 0; n < words.length; i++) {
  148. var testWidth = context.measureText(words[i]);
  149. if (testWidth > maxWidth)
  150. maxWidth = metrics.width;
  151. }
  152. // Arrange all words into lines
  153. for (var n = 0; n < words.length; n++) {
  154. var testLine = line + words[n];
  155. var testWidth = context.measureText(testLine).width;
  156. if (testWidth > maxWidth) {
  157. lines[currentLine] = line;
  158. currentLine++;
  159. line = words[n] + " ";
  160. }
  161. else {
  162. line = testLine + " ";
  163. }
  164. }
  165. lines[currentLine] = line;
  166. // Plot frame and background
  167. if (lines.length > 1) {
  168. // Multiline text
  169. plotTextBackground(context, x - 2, y - lineHeight / 2 - 2, maxWidth + 3, lines.length * lineHeight + 3);
  170. }
  171. else {
  172. // Single line text
  173. var textWidth = context.measureText(lines[0]).width; // Limit frame width to the actual line width
  174. plotTextBackground(context, x - 2, y - lineHeight / 2 - 2, textWidth + 3, lines.length * lineHeight + 3);
  175. }
  176. // Output lines of text
  177. context.fillStyle = "black";
  178. for (var n = 0; n < lines.length; n++) {
  179. context.fillText(" " + lines[n], x, yCurrent);
  180. yCurrent += lineHeight;
  181. }
  182. }
  183. </script>
  184. <div>
  185. <div id="map"></div>
  186. </div>

Listing 25.

Also add the ActionLink on the Index.cshtml page to access the Contor Series view.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("MarketTemplate", "Home")">
  8. Market Template
  9. </a>
  10. </li>
  11. </ul>

Listing 26.

Now run the application in web browser and open the page.

ASP.NET Core
Figure 17.

Polyline Series

Add the action method in side the HomeController for PolylineSeries Map and add the following code.

  1. public IActionResult PolylineSeries()
  2. {
  3. return View();
  4. }

Listing 24.

Now right click on the PolylineSeries action method and click add view to add a view for this action method. A View for PolylineSeries will get added at the following location in the project “\Views\Home\PolylineSeries.cshtml”

Add the following html and script in side the “PolylineSeries.cshtml” for the rendering the market template map type.

  1. @{
  2. ViewData["Title"] = "Polyline Series";
  3. }
  4. <script src="https://www.igniteui.com/js/map-helper.js" type="text/javascript"></script>
  5. <style>
  6. #tooltipTable {
  7. font-family: Verdana, Arial, Helvetica, sans-serif;
  8. width: 100%;
  9. border-collapse: collapse;
  10. }
  11. #tooltipTable td, #tooltipTable th {
  12. font-size: 9px;
  13. border: 1px solid #5da7df;
  14. padding: 3px 7px 2px 7px;
  15. }
  16. #tooltipTable th {
  17. font-weight: bold;
  18. font-size: 11px;
  19. text-align: left;
  20. padding-top: 5px;
  21. padding-bottom: 4px;
  22. background-color: #5da7df;
  23. color: #ffffff;
  24. }
  25. </style>
  26. <script id="geoSymbolTooltip" type="text/x-jquery-tmpl">
  27. <table id="tooltipTable">
  28. <tr><th colspan="2">${item.City}, ${item.Country}</th></tr>
  29. <tr>
  30. <td>Population:</td>
  31. <td>${item.Population}</td>
  32. </tr>
  33. </table>
  34. </script>
  35. <h2>Polyline Series</h2>
  36. <script type="text/javascript">
  37. $(function () {
  38. // load geo-locations from shapefile and simplify data structure
  39. var worldAirports = [];
  40. var worldConnections = [];
  41. var worldCities = [];
  42. var points = [];
  43. var shapeDataSource = new $.ig.ShapeDataSource({
  44. shapefileSource: "https://www.igniteui.com/data-files/shapes/world_cities.shp",
  45. databaseSource: "https://www.igniteui.com/data-files/shapes/world_cities.dbf",
  46. transformRecord: function (rec) {
  47. var pointX = rec.points.item(0).item(0).x();
  48. var pointY = rec.points.item(0).item(0).y();
  49. var city = rec.fields.item("NAME");
  50. var country = rec.fields.item("COUNTRY");
  51. var population = rec.fields.item("POPULATION");
  52. if (population > 0) {
  53. points.push({
  54. Latitude: pointY, Longitude: pointX,
  55. City: city, Country: country,
  56. Population: population
  57. });
  58. }
  59. },
  60. // process data and create map when finished loading shapefile
  61. callback: function () {
  62. worldCities = points;
  63. generateConnections();
  64. createMap();
  65. }
  66. });
  67. shapeDataSource.dataBind();
  68. // generate flight paths and airport connections
  69. function generateConnections() {
  70. var count = worldCities.length;
  71. var airports = [];
  72. var flights = [];
  73. var minDistance = 200;
  74. var maxDistance = 7000;
  75. var maxConnections = 300;
  76. var total = 0;
  77. for (var i = 0; i < count; i++) {
  78. var connections = [];
  79. var origin = worldCities[i];
  80. for (var ii = 0; ii < count; ii++) {
  81. var dest = worldCities[ii];
  82. if (origin.City != dest.City &&
  83. origin.Population > dest.Population &&
  84. origin.Country == "US" &&
  85. dest.Country == "US") {
  86. var hasConnection = false;
  87. var distance = calcGeoDistance(origin, dest);
  88. if (distance > minDistance &&
  89. distance < maxDistance) {
  90. if (origin.Population > 3000000 &&
  91. dest.Population > 500000 && distance < maxDistance) {
  92. hasConnection = true;
  93. }
  94. // optional code to add more connections
  95. //if (origin.Population > 1000000 &&
  96. // dest.Population > 250000 && distance < maxDistance * 0.75) {
  97. // hasConnection = true;
  98. //}
  99. //if (origin.Population > 500000 &&
  100. // dest.Population > 100000 && distance < maxDistance * 0.5) {
  101. // hasConnection = true;
  102. //}
  103. }
  104. if (hasConnection) {
  105. var path = [];
  106. path = calcGeoPath(origin, dest);
  107. connections.push(path);
  108. airports.push(dest);
  109. total++;
  110. }
  111. }
  112. }
  113. if (connections.length > 0) {
  114. flights.push({ city: origin, points: connections });
  115. airports.push(origin);
  116. }
  117. if (total > maxConnections) break;
  118. }
  119. worldConnections = flights;
  120. worldAirports = airports;
  121. }
  122. // calculate geo-path between two locations
  123. function calcGeoPath(origin, dest) {
  124. var interval = 200;
  125. var path = [];
  126. var distance = calcGeoDistance(origin, dest);
  127. if (distance <= interval) {
  128. path.push({ x: origin.Longitude, y: origin.Latitude });
  129. path.push({ x: dest.Longitude, y: dest.Latitude });
  130. } else {
  131. var current = origin;
  132. for (var dist = interval; dist <= distance; dist += interval) {
  133. path.push({ x: current.Longitude, y: current.Latitude });
  134. var bearing = calcBearing(current, dest);
  135. current = calcDestination(current, bearing, interval);
  136. }
  137. path.push({ x: dest.Longitude, y: dest.Latitude });
  138. }
  139. return path;
  140. }
  141. // calculate bearing angle between two locations
  142. function calcBearing(origin, dest) {
  143. origin = toRadianLocation(origin);
  144. dest = toRadianLocation(dest);
  145. //var dLat = (dest.Latitude - origin.Latitude);
  146. var dLon = (dest.Longitude - origin.Longitude);
  147. var y = Math.sin(dLon) * Math.cos(dest.Latitude);
  148. var x = Math.cos(origin.Latitude) * Math.sin(dest.Latitude) -
  149. Math.sin(origin.Latitude) * Math.cos(dest.Latitude) * Math.cos(dLon);
  150. var angle = Math.atan2(y, x);
  151. return toDegreesNormalized(angle);
  152. }
  153. // calculate destination for origin location and travel distance
  154. function calcDestination(origin, bearing, distance) {
  155. var radius = 6371.0;
  156. origin = toRadianLocation(origin);
  157. bearing = toRadians(bearing);
  158. distance = distance / radius; // angular distance in radians
  159. var latitude = Math.asin(Math.sin(origin.Latitude) * Math.cos(distance) +
  160. Math.cos(origin.Latitude) * Math.sin(distance) * Math.cos(bearing));
  161. var x = Math.sin(bearing) * Math.sin(distance) * Math.cos(origin.Latitude);
  162. var y = Math.cos(distance) - Math.sin(origin.Latitude) * Math.sin(origin.Latitude);
  163. var longitude = origin.Longitude + Math.atan2(x, y);
  164. longitude = (longitude + 3 * Math.PI) % (2 * Math.PI) - Math.PI; // normalize to -180..+180º
  165. longitude = toDegrees(longitude);
  166. latitude = toDegrees(latitude);
  167. return { Longitude: longitude, Latitude: latitude };
  168. }
  169. // calculate distance between two locations
  170. function calcGeoDistance(origin, dest) {
  171. origin = toRadianLocation(origin);
  172. dest = toRadianLocation(dest);
  173. var sinProd = Math.sin(origin.Latitude) * Math.sin(dest.Latitude);
  174. var cosProd = Math.cos(origin.Latitude) * Math.cos(dest.Latitude);
  175. var dLon = (dest.Longitude - origin.Longitude);
  176. var angle = Math.acos(sinProd + cosProd * Math.cos(dLon));
  177. var distance = angle * 6371.0;
  178. return distance; // * 6371.0; // in km
  179. }
  180. function toRadianLocation(geoPoint) {
  181. var longitude = toRadians(geoPoint.Longitude);
  182. var latitude = toRadians(geoPoint.Latitude);
  183. return { Longitude: longitude, Latitude: latitude };
  184. }
  185. function toRadians(degrees) {
  186. return degrees * Math.PI / 180;
  187. }
  188. function toDegrees(radians) {
  189. return (radians * 180.0 / Math.PI);
  190. }
  191. function toDegreesNormalized(radians) {
  192. var degrees = toDegrees(radians);
  193. degrees = (degrees + 360) % 360;
  194. return degrees;
  195. }
  196. function createMap() {
  197. $("#map").igMap({
  198. width: "700px",
  199. height: "500px",
  200. overviewPlusDetailPaneVisibility: "visible",
  201. overviewPlusDetailPaneBackgroundImageUri: "https://www.igniteui.com/images/samples/maps/world.png",
  202. windowRect: {
  203. left: 0.180,
  204. top: 0.3143,
  205. width: 0.180,
  206. height: 0.180
  207. },
  208. series: [{
  209. type: "geographicPolyline",
  210. name: "worldConnections",
  211. shapeMemberPath: "points",
  212. dataSource: worldConnections,
  213. shapeFilterResolution: 5.0,
  214. outline: "rgba(20,20,20,0.5)",
  215. thickness: 0.75,
  216. showTooltip: false,
  217. },
  218. {
  219. type: "geographicSymbol",
  220. name: "worldAirports",
  221. dataSource: worldAirports,
  222. longitudeMemberPath: "Longitude",
  223. latitudeMemberPath: "Latitude",
  224. markerType: "circle",
  225. markerCollisionAvoidance: "fade",
  226. markerOutline: "rgba(90,160,220,0.9)",
  227. markerBrush: "rgba(90,160,220,0.7)",
  228. showTooltip: true,
  229. tooltipTemplate: "geoSymbolTooltip",
  230. }
  231. ]
  232. });
  233. $("#map").find(".ui-widget-content").append("<span class='copyright-notice'><a href='https://www.openstreetmap.org/copyright'>© OpenStreetMap contributors</a></span>");
  234. };
  235. });
  236. </script>
  237. <div>
  238. <div id="map"></div>
  239. </div>

Listing 25.

Also add the ActionLink on the Index.cshtml page to access the Contor Series view.

  1. @{
  2. ViewData["Title"] = "Index";
  3. }
  4. <h2>Types of Maps</h2>
  5. <ul>
  6. <li>
  7. <a href="@Url.Action("Polyline", "Home")">
  8. Polyline Series
  9. </a>
  10. </li>
  11. </ul>

Listing 26.

Now run the application in web browser and open the page.

ASP.NET Core
Figure 17.

Summary

Using map in web application is very useful it can be use as a an actual map for representing the geographic locations. There are other use of map in web applications like Checking for certain spots like restaurants, cafe. Other use is monitoring the traffics, In e-commerce applications providing the location based discounts to increase sale. Tracking the physical activities like running, walk etc. Calculating cost of shipping charge. There are many ways to use the maps that will certainly add the benefits to business.

Learn more, download a free trial, and get started here,