Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2210
  • Last Modified:

Getting latitude and longitude from an address/postal code string

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?
  • 2
1 Solution
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.
tekticianAuthor Commented:
Yea I have been looking for an external service.

I'm making a request to geocoder.ca for now.

It seems to be working with the exception that the look ups are a little inaccurate sometimes.
tekticianAuthor Commented:
I find that using the Yahoo Geocode service is a lot more accurate than geocoder.ca.

I am still interested in taking on a method that uses the Map class of Virtual Earth in asp.net.  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 = "http://local.yahooapis.com/MapsService/V1/geocode?appid=YahooAppID&location=";
        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


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now