SIGN UP MEMBER LOGIN:    
ARTICLE

Using Google Map in an ASP.NET MVC Application

Posted by Krishna Garad Articles | ASP.NET MVC with C# January 30, 2012
In this article we will see how to show a Google Map in an ASP.NET MVC application.
Reader Level:
Download Files:
 

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 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 tagprefix 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 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 web.confige file so change the your API key in web.confige 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.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks All of you

Posted by Krishna Garad Feb 01, 2012

Really this article is valuable

Posted by Arjun Panwar Feb 01, 2012

Hi nice article.pls do this demp in MVC Razor that is better and can developed GIS app using google map

Posted by Dinkar Chavhan Feb 01, 2012

Hi Krishna. You have presented your article very nicely.

Posted by Monika Arora Jan 31, 2012

thankss for sharing

Posted by Sonakshi Singh Jan 31, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor