Saturday 17 September 2011

Playing with Google maps in Asp.net

1. Get a Google Maps API key from here:
http://www.google.com/apis/maps/
2. Download the SubGurim wrapper dll from here:
http://en.googlemaps.subgurim.net/descargar.aspx
3. Unzip it, and put it in your \bin directory
4. Add it to your toolbox by
Tools -> Choose Toolbox Items -> Browse -> Select the .dll file -> OK
GMap will now be in the ‘Standard’ area of your Toolbox.
5. Add a new webpage.
6. Drag the GMap from the toolbox onto the page. A new instance of the GMap will appear on the page, with the name ‘GMap1′
7. Add the following lines to your web.config file:

<appSettings>
<add key="googlemaps.subgurim.net"
 value = "YourGoogleMapsAPIKeyHere" />
</appSettings>

Default.aspx
<%@ Register Assembly="GMaps" Namespace="Subgurim.Controles" TagPrefix="cc1" %>
<cc1:GMap ID="GMap2" runat="server" />

Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            MapMyLocation(“Hyderabad”,”Ankur Bhatnagar”);

    }

private void MapMyLocation(string address,string name)
    {
        Subgurim.Controles.GeoCode GeoCode;
        GeoCode = GMap2.getGeoCodeRequest(address, ConfigurationManager.AppSettings["googlemaps.subgurim.net"]
.ToString());
        Subgurim.Controles.GLatLng gLatLng = new Subgurim.Controles.GLatLng(GeoCode.Placemark.coordinates.lat, GeoCode.Placemark.coordinates.lng);

        GMap2.setCenter(gLatLng, 16, Subgurim.Controles.GMapType.GTypes.Normal);
        Subgurim.Controles.GMarker oMarker = new Subgurim.Controles.GMarker(gLatLng);
        GMap2.addGMarker(oMarker);
        GMap2.ToolTip = name;
        var omarker = new Subgurim.Controles.GMarker(gLatLng);
        GInfoWindow gmapinfo = new GInfoWindow(omarker, "<center><b>"+name+"<br/>"+address+"</b></center>", true);
        GMap2.addInfoWindow(gmapinfo);
    }

This sample will give your location in Google maps. Give your location and name in page load in place of my location and name.

Thanks.

No comments:

Post a Comment