Leaflet JS is a very popular lightweight js library for displaying data in a map and creating an interactive map. It is quite flexible in terms to adding custom layers, styling controls, and integrating events. In this post, I will try to demonstrate how it can be integrated with your MVC application instead using Google map API. I will also integrate the clustering since, in my application, there is a possibility to have multiple records with the same latitude and longitude. I guess it will be easier to understand if you have some basic knowledge on map marker clustering. You can read more about map clustering at https://developers.google.com/maps/documentation/javascript/marker-clustering .
The finished product will be like the image below –
Setup Model
In the MVC application, I have created a model which holds the GPS coordinates along with other fields. Just to make it simple, I have omitted unnecessary codes to make it simple. Here is my model.
[Display(Name = "Description"), MaxLength(8000, ErrorMessage = "Title cannot be more than 8000 characters long."), Required(ErrorMessage ="You must enter a description in {0} field")]
09
[AllowHtml]
10
[DataType(DataType.MultilineText)]
11
publicstringDescription { get; set; }
12
13
[Display(Name = "Park/Location Name"), MaxLength(120, ErrorMessage = "Park or Location name is too long"), Required]
[note title=”Optional Title”]In my application I have another model which links with this model. Basically, I am using a relational database. But just to make this demonstration simple, I am just using the relevant model. [/note]
Setup Controller
In my controller, I am using a get request to fetch data in JSON format so that I can bind them in the Leaflet map. Here is the sample code of the controller. The Map action result produces the JSON data which is accessible through GET request.
var q= db.Alerts.Where(a => a.Published == "Yes").ToList();
7
//return the result in json
8
returnJson(q, JsonRequestBehavior.AllowGet);
9
}
Setup View
Again, I have omitted irrelevant codes from the view. I am interested in the map view section of the page where the Map will be loaded through ajax call. The basic HTML code has been used in the bootstrap tab. In the below code block, you might have noticed an overlay block which has been used to show loading indicator to end user while fetching the data from the remote server. It will hide through CSS class switcher once data has been successfully loaded.
In the header section, let’s call the all associated plugins. Remember you must require jQuery and Bootstrap js libraries and style sheets to work with this demo.
I have included Google API key since I am using Google layer instead of OpenStreet map in this example. Same output of the json data.
The idea is to load the map only when a user clicks on the map tab. I don’t want to load all the content on initial page load since it can increase the payload and user might have to wait longer to load the page. To give better user experience, the page will load data on demand means when user requests for the additional data.
Place the above codes inside the script block. Remember I have not shown the jQuery and Bootstrap js files in the above script reference since I have added them in the layout file.
In the above javascript, I have to create another function to clear the map. It is a bit tricky to display map inside a bootstrap tab or Modal. If you are using this map in a single page then you don’t have to do this. You can clean up your code.
Hi Ernest,
This is part of big project. I hope I have explained in details on my post so that you can build it by yourself. Let me know if you struggle at any stage.
Hi, dear sir.
I’m in Vietnam, I’m not an IT person. But I want to make a website about maps myself, to track the changes of my devices located at various points.
I’m very impressed with your article, but I haven’t been able to implement myself how to load data from SQL partial to web according to menu options. Also how to update data in real time.
Hope you can give me some sample code, so I can do it myself.
Thank you very much.
Excellent article, just what I need.
Could you please the visual studio solution source code available for this example?
Very good article,
Where I can get the visual studio solution source code ?
can i get the visual studio code
Hello,could you share the VS source code.
Would kindly share the full source code
Hi Ernest,
This is part of big project. I hope I have explained in details on my post so that you can build it by yourself. Let me know if you struggle at any stage.
Good luck.
Hi, dear sir.
I’m in Vietnam, I’m not an IT person. But I want to make a website about maps myself, to track the changes of my devices located at various points.
I’m very impressed with your article, but I haven’t been able to implement myself how to load data from SQL partial to web according to menu options. Also how to update data in real time.
Hope you can give me some sample code, so I can do it myself.
Thank you very much.