Click here to Skip to main content
15,896,557 members
Articles / Web Development / ASP.NET

Google Maps API V3 for ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (108 votes)
30 Dec 2011CPOL4 min read 615.8K   45.7K   267  
The most frequently used tasks in Google Maps. The article includes an explanation of geocoding and reverse geocoding both in JavaScript and C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml.Linq;

public partial class GeoCodingCsharp : System.Web.UI.Page
{
    static string baseUri = "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false";
    string location = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    public static Coordinate GetCoordinates(string region)
    {
        using (var client = new WebClient())
        {

            string uri = "http://maps.google.com/maps/geo?q='" + region + "'&output=csv&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA";

            string[] geocodeInfo = client.DownloadString(uri).Split(',');

            return new Coordinate(Convert.ToDouble(geocodeInfo[2]), Convert.ToDouble(geocodeInfo[3]));
        }
    }

    public struct Coordinate
    {
        private double lat;
        private double lng;

        public Coordinate(double latitude, double longitude)
        {
            lat = latitude;
            lng = longitude;

        }

        public double Latitude { get { return lat; } set { lat = value; } }
        public double Longitude { get { return lng; } set { lng = value; } }

    }

    //Reverse Gecoding
    public static void RetrieveFormatedAddress(string lat, string lng)
    {
        string requestUri = string.Format(baseUri, lat, lng);

        using (WebClient wc = new WebClient())
        {
            string result = wc.DownloadString(requestUri);
            var xmlElm = XElement.Parse(result);
            var status = (from elm in xmlElm.Descendants() where elm.Name == "status" select elm).FirstOrDefault();
            if (status.Value.ToLower() == "ok")
            {
                var res = (from elm in xmlElm.Descendants() where elm.Name == "formatted_address" select elm).FirstOrDefault();
                requestUri = res.Value;
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        double Lat = GetCoordinates("Hyderabad").Latitude;
        double Lng = GetCoordinates("Hyderabad").Longitude;
        Button1.Attributes.Add("onclick", "return FindLocaiton('"+Lat+"','"+Lng+"') ");
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Collabera
Singapore Singapore
S V Sai Chandra is a Software Engineer from Hyderabad Deccan. He started Embedded Programing in his college days and now he is a Web Developer by Profession. He Loves coding and his passion has always been towards Microsoft Technologies. Apart from coding his other hobbies include reading books, painting and hang out with friends is his most favorite past time hobby.
He blogs at
http://technowallet.blogspot.com
Technical Skills:
C#,Ado.Net,Asp.Net,Sql Server,JavaScript,XML,Web services.

Comments and Discussions