Getting latitude and longitude from an address/postal code string

Posted on 2008-11-03
Last Modified: 2013-11-08
I have a database with postal code and address fields in a user table.  I want to plot these into a Virtual Earth Map control in Visual Studio but it seems like I can only give it a longitude/latitude as the parameters.

This table grows dynamically so I can't statically plot all the long/lat data myself in the table.

Is there a way that I can programatically (C#) get the long/lat from just a postal/zip code or address string before creating the markers on the maps?
Question by:tektician
    LVL 4

    Expert Comment

    If the Virtual Earth control has no post code lookup facility then you will have to get the information from an external database.

    I know when you run an address/post code search on Live Maps it refers to the location by latitude and longitude in the "cp" query string variable.when you click on share.
    There may be a way of doing this programatically in order to get the information you require.

    Author Comment

    Yea I have been looking for an external service.

    I'm making a request to for now.

    It seems to be working with the exception that the look ups are a little inaccurate sometimes.

    Accepted Solution

    I find that using the Yahoo Geocode service is a lot more accurate than

    I am still interested in taking on a method that uses the Map class of Virtual Earth in  However, if anyone is interested in the Yahoo Geocode method, I made a sample look up in the following code:

            double longitude = 0;
            double latitude = 0;
            string url = "";
            string postalCode = "V5T 1V9";
            url += postalCode;
            WebResponse response;
            WebRequest request = WebRequest.Create(new Uri(url));
            response = request.GetResponse();
            XmlTextReader reader = new XmlTextReader(response.GetResponseStream());
            XNamespace xmlns = XNamespace.Get("urn:yahoo:maps");
            var loc = (from ll in XDocument.Load(reader).Elements(xmlns + "ResultSet").Elements(xmlns + "Result")
                      select new
                          Latitude = ll.Element(xmlns + "Latitude").Value,
                          Longitude = ll.Element(xmlns + "Longitude").Value
            latitude = Convert.ToDouble(loc.Latitude);
            longitude = Convert.ToDouble(loc.Longitude);
            LatLongWithAltitude l = new LatLongWithAltitude(latitude, longitude);
            Shape shape = new Shape(ShapeType.Pushpin, l);
            shape.Title = "Test";

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now