Introduction
In this article, we will see how to show a Google Map in an ASP.NET MVC application. Basically in our application, we need to show the position of the user on the map. To display the user position we will use Google Map in our ASP.NET MVC web application. So let's proceed to show the Google Map in our MVC application.
Step 1. First, we have to register our Google Map API key so register your own key here and also download the Gmaps.dll from there.
Step 2. Now create your ASP.NET MVC application and add the controller in your application as well as add the view.
Step 3. Now add the reference to downloaded Gmaps.dll by right-clicking on the solution.
Step 4. Next, we have to register the tag prefix and import the namespace of G-map like below.
<%@ Register assembly="GMaps" namespace="Subgurim.Controles" tagprefix="cc1" %>
<%@ Import Namespace="Subgurim.Controles" %>Step 5. Next, write the script with runat=server attribute in <head></head> section for containing Page_Load. We will show the map in the page_load event; write down the following code in the script to load the map by giving the city, state, and country information.
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SeeMap("Hyderabad", "Andhra Pradesh", "India");
}
}
private void SeeMap(string _city, string _state, string _country)
{
string fulladdress = string.Format("{0}.{1}.{2}.{3}", "Assembly", _city, _state, _country);
//change your api key in web.confige file
string skey = ConfigurationManager.AppSettings["googlemaps.subgurim.net"];
GeoCode geocode;
geocode = GMap1.getGeoCodeRequest(fulladdress);
var glatlng = new Subgurim.Controles.GLatLng(geocode.Placemark.coordinates.lat, geocode.Placemark.coordinates.lng);
GMap1.setCenter(glatlng, 16, Subgurim.Controles.GMapType.GTypes.Normal);
var oMarker = new Subgurim.Controles.GMarker(glatlng);
GMap1.addGMarker(oMarker);
}
</script>In the above lines of code, we loaded the map by passing city, state, and country information. In the above code, we are loading the API key from the web. configure file so change your API key on the web. configure file.
Step 6. Next, put the Gmap control in the <body></body> section on your aspx page like below.
<cc1:GMap ID="GMap1" runat="server" Height="300px" Width="300px" />Step 7. Now run your application and see the map.
Conclusion
In this easy way, we can show the map on our ASP.NET MVC view.


chaitra DPosted Oct 9, 2017, 2:38 AM EDT
Hello sir i am getting an error like The remote server returned an error: (404) Not Found. can anyone please help me
Satyaprakash SamantarayPosted May 25, 2017, 3:13 AM EDT
Where is output ????
Anu VPosted Oct 25, 2016, 10:56 PM EDT
Nice...
Rupali ShindePosted Jan 8, 2016, 1:46 AM EST
thanks for sharing ..
Sachin DPosted May 1, 2013, 5:22 AM EDT
I working in Asp.net website and I'm trying to use polylines to display history of particular asset and having values in database table like Latitude,Longitude,Asset_Id in transaction table. And i used internet for all this and getting only Mvcarray and jsonarray and I'm using Website. if any steps to add Mvc in website please help me....
ha DangPosted Jun 27, 2012, 11:47 AM EDT
when i run my app it show a error: The type or namespace name 'dynamic' could not be found (are you missing a using directive or an assembly reference?) in : <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> I dont understand, can u help me! thank u so much!
Krishna GaradPosted Jun 26, 2012, 4:25 AM EDT
May your key is invalid. check the key which you recieved from gmap
ha DangPosted Jun 25, 2012, 3:44 AM EDT
I use VS 2008 and MVC 2 and I try to build a application for show map in a ASP.net MVC but when i run my application. it dont show map on browser. can u help me?
Arjun PanwarPosted Feb 1, 2012, 6:27 AM EST
Really this article is valuable
Dinkar ChavhanPosted Jan 31, 2012, 7:11 PM EST
Hi nice article.pls do this demp in MVC Razor that is better and can developed GIS app using google map
Monika AroraPosted Jan 31, 2012, 6:00 PM EST
Hi Krishna. You have presented your article very nicely.
Sonakshi SinghPosted Jan 31, 2012, 5:50 PM EST
thankss for sharing