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
- 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.IEnumerable get List of table values. Select query will return set of values and JsonResult post data to ajax request using HttpPost keyword.
- 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();
- // GET: Maps
- 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
- }
- }
View
Write the following reference for Leaflet Js and css because it is an important reference to mention.Map div tag,- <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>
Complete code- <div id="map" style="height: 440px; border: 1px solid #AAA;"></div>
- @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:

Ayush gabaPosted Nov 5, 2019, 4:15 AM
Hi I am getting these values from controller Title = Convert.ToString(row["Location"]), Latitude = Convert.ToDouble(row["Latitude"]), Longitude = Convert.ToDouble(row["Longitude"]), when value fetch from view its showing data[i] is not defined for (var i = 0; i < result.length; ++i) { var popup = '<b>Name:</b> ' + data[i].Description + '<br/><b>Latitude:</b> ' + data[i].Latitude + '<br/><b>Longitude:</b> ' + data[i].Longitude + '<br/><b>Location:</b> ' + data[i].Title; L.marker([data[i].Latitude, data[i].Longitude]).bindPopup(popup).addTo(map); IN jquery its showing me data[i] is undefined Description = Convert.ToString(row["Description"])
Manisha MaliPosted Mar 22, 2019, 12:54 AM
When i execute View it only show a box .Uncaught ReferenceError: L is not defined at HTMLDocument.<anonymous> (Index:161) at fire (jquery-1.12.4.js:3232) at Object.fireWith [as resolveWith] (jquery-1.12.4.js:3362) at Function.ready (jquery-1.12.4.js:3582) at HTMLDocument.completed (jquery-1.12.4.js:3617)
Gol MaryPosted Dec 29, 2017, 4:19 PM
Can not display map for me :(
Manav PandyaPosted Jun 6, 2017, 6:23 AM
Nice one ..............
SubashPosted Jul 4, 2016, 4:19 AM
Good
Vinay SinghPosted Jun 21, 2016, 5:16 AM
Nice one
Sr KarthigaPosted Apr 22, 2016, 7:56 AM
good one
Sthitaprajnya Debasis NayakPosted Mar 22, 2016, 7:00 AM
Super Sir !!!
Sibeesh VenuPosted Mar 22, 2016, 6:18 AM
Nice Share
Rajeev PunhaniPosted Mar 22, 2016, 1:08 AM
Nice.
Pradeep SahooPosted Mar 21, 2016, 9:50 PM
Nice info. thanks for sharing
Rajeesh MenothPosted Mar 21, 2016, 11:17 AM
Good One! There have any limitation for this Opensource ?
Humayun Kabir MamunPosted Mar 21, 2016, 9:40 AM
Nice...
Vignesh ManiPosted Mar 21, 2016, 9:25 AM
Good one
Mohammed IbrahimPosted Mar 21, 2016, 8:33 AM
nice