Introduction
Clicking marker of user location displays the user details. This request is based on Ajax method and JsonResult. OpenStreetMap is open data and you are free to use it for any purpose.
Pre Requisite
- Windows 7 or above
- SQL Server 2012 or above
- ASP.NET MVC 5 above framework
Steps
- Create a table as in the following:
- Create a new MVC project and name it WebApplication.
- Right click model folder and create a new item ADO.NET Entity Data model. Give it a name and then click EF Designer From Data.
Here's the screenshot:
Select given table and click finish and edmx gets created.
Generated connection automatically creates edmx from database
- Write then following code into controller.
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using WebApplication1.Models;
-
-
- namespace WebApplication1.Controllers
- {
- public class MapsController : Controller
- {
- private test2Entities db = new test2Entities();
-
-
- public ActionResult Index()
- {
- return View(db.Maps.ToList());
- }
-
- #region [Map]
- [HttpPost]
- public JsonResult GetMap()
- {
- var data1 = Map();
- return Json(data1, JsonRequestBehavior.AllowGet);
- }
- public IEnumerable<Map> Map()
- {
-
- return (from p in db.Maps
- select new
- {
- Name = p.Name,
- Latitude = p.Latitude,
- Longitude = p.Longitude,
- Location = p.Location,
- Description = p.Description,
- Id = p.Id
- }).ToList()
- .Select(res => new Map
- {
- Name = res.Name,
- Latitude = res.Latitude,
- Longitude = res.Longitude,
- Location = res.Location,
- Description = res.Description,
- Id = res.Id
-
-
- });
-
- }
- #endregion
-
- }
- }
IEnumerable get List of table values. Select query will return set of values and JsonResult post data to ajax request using HttpPost keyword.
View
Write the following reference for Leaflet Js and css because it is an important reference to mention.
- <link rel="stylesheet" type="text/css" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
- <script type='text/javascript' src='http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js?2'></script>
-
- <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
Map div tag,
- <div id="map" style="height: 440px; border: 1px solid #AAA;"></div>
Complete code
- @model IEnumerable<WebApplication1.Models.Map>
-
- @{
- Layout = null;
- }
-
- <link rel="stylesheet" type="text/css" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
- <script type='text/javascript' src='http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js?2'></script>
-
- <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
-
- <div id="map" style="height: 440px; border: 1px solid #AAA;"></div>
-
- <script type="text/javascript">
-
- $(document).ready(function () {
- var map = L.map('map', {
-
- center: [10.1102278, 77.8958904],
- minZoom: 4,
- zoom: 6
- });
- $.ajax({
- type: "POST",
- url: '/Maps/GetMap',
- success: function (data) {
- var result = JSON.stringify(data);
-
- for (var i = 0; i < result.length; ++i) {
- var popup ='<b>Name:</b> '+ data[i].Name +
- '<br/><b>Latitude:</b> ' + data[i].Latitude +
- '<br/><b>Longitude:</b> ' + data[i].Longitude+
- '<br/><b>Location:</b> ' + data[i].Location;
-
-
- L.marker([data[i].Latitude, data[i].Longitude])
- .bindPopup(popup)
- .addTo(map);
- }
-
- },
- error: function (xhr) {
-
- console.log(xhr.responseText);
- alert("Error has occurred..");
- }
- });
-
- L.tileLayer('http://{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.png', {
- attribution: '© <a href="http://osm.org/copyright" title="OpenStreetMap" target="_blank">OpenStreetMap</a> contributors | Tiles Courtesy of <a href="http://www.mapquest.com/" title="MapQuest" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png" width="16" height="16">',
- subdomains: ['otile1', 'otile2', 'otile3', 'otile4']
- }).addTo(map);
-
- });
-
- </script>
- The Div id "map" gets the following functions for showing the map. This map has a default view latitude and longitude using centerkey word, and user can change zoom level also.
- Ajax method retrieves latitude and longitude and the retrieved values for map using marker and bind popup method show the user details on clicking this marker.
Output
Summary
We learned how to get Osm Map user details and location based on latitude and longitude from database using CRUD Functionality with the Entity Framework in ASP.NET MVC and Leaflet. I hope this article is useful for all .NET beginners.
Read more articles on ASP.NET: