google mapping, asp.net, c#

Can you show me where I can get a code to convert from zip code or address to get
latitude/longitude in asp.net/c#?

latitude/longitude is all I need to know. I know there is google xml that I can use. but need some helps to get latitude/longitude.

Thanks,
solution1368Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I don't believe you will find this in any web scripting code.  The lat/lon for a polygon is based on the population centroid.   You would get this from the census.  However, it looks like due to the govt shutdown, it is not available.  http://www.census.gov/tiger/tms/gazetteer/zips.txt

There are plenty of sources to purchase data from if you are interested.
0
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi..

If you want to use the Google Maps API have a look at their REST API, you don't need to install a Google Maps API just send a Request like

http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

and you will get a response XML.

For more Information have a look at

https://developers.google.com/maps/documentation/geocoding/index#GeocodingRequests
0
käµfm³d 👽Commented:
*No Points*

To add to samirbhogayta's comment, it does appear that you can send simply the zip code in to Google's API as well.

e.g.

Request
http://maps.googleapis.com/maps/api/geocode/xml?address=94043&sensor=false

Open in new window


Response
<GeocodeResponse>
  <status>OK</status>
  <result>
    <type>postal_code</type>
    <formatted_address>Mountain View, CA 94043, USA</formatted_address>
    <address_component>
      <long_name>94043</long_name>
      <short_name>94043</short_name>
      <type>postal_code</type>
    </address_component>
    <address_component>
      <long_name>Mountain View</long_name>
      <short_name>Mountain View</short_name>
      <type>locality</type>
      <type>political</type>
    </address_component>
    <address_component>
      <long_name>California</long_name>
      <short_name>CA</short_name>
      <type>administrative_area_level_1</type>
      <type>political</type>
    </address_component>
    <address_component>
      <long_name>United States</long_name>
      <short_name>US</short_name>
      <type>country</type>
      <type>political</type>
    </address_component>
    <geometry>
      <location>
        <lat>37.4284340</lat>
        <lng>-122.0723816</lng>
      </location>
      <location_type>APPROXIMATE</location_type>
      <viewport>
        <southwest>
          <lat>37.3857439</lat>
          <lng>-122.1084200</lng>
        </southwest>
        <northeast>
          <lat>37.4640869</lat>
          <lng>-122.0359899</lng>
        </northeast>
      </viewport>
      <bounds>
        <southwest>
          <lat>37.3857439</lat>
          <lng>-122.1084200</lng>
        </southwest>
        <northeast>
          <lat>37.4640869</lat>
          <lng>-122.0359899</lng>
        </northeast>
      </bounds>
    </geometry>
  </result>
</GeocodeResponse>

Open in new window

0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

solution1368Author Commented:
but how to capture lat / lng from the xml is my primary question (in asp.net/c#)
0
käµfm³d 👽Commented:
Load it into an XmlDocument, and use XPath to get the values.

e.g.

using System;

namespace ConsoleApplication170
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
            string xmlResponse;
            System.Xml.XmlNode gemoetry;
            System.Xml.XmlNode location;
            System.Xml.XmlNode lat;
            System.Xml.XmlNode lng;

            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                xmlResponse = client.DownloadString("http://maps.googleapis.com/maps/api/geocode/xml?address=94043&sensor=false");
            }

            xdoc.LoadXml(xmlResponse);
            gemoetry = xdoc.SelectSingleNode("/GeocodeResponse/result/geometry");
            location = gemoetry.SelectSingleNode("location");
            lat = location.SelectSingleNode("lat");
            lng = location.SelectSingleNode("lng");

            string latitude = lat.InnerText;
            string longtitude = lng.InnerText;

            Console.WriteLine("{0}, {1}", latitude, longtitude);
            Console.ReadKey();
        }
    }
}

Open in new window

0
solution1368Author Commented:
public string GetLocationConverter(string zip)
        {
            string url = "http://maps.google.com/maps/api/geocode/xml?address=" + zip + "&sensor=false";
            XDocument doc = XDocument.Load(url);
            string value = string.Empty;
            string lat = string.Empty;
            string lng = string.Empty;
            foreach (XElement element in doc.Element("GeocodeResponse").Elements("result").Elements("geometry").Elements("location").Descendants())
                {

                    value = element("lat");
                }
            return value;
            //throw new NotImplementedException();
        }

I have this one but just try to get "lat" and the code is not working.

Can you help?
0
käµfm³d 👽Commented:
If you want to use LINQ, then:

XDocument xdoc = XDocument.Load("http://maps.googleapis.com/maps/api/geocode/xml?address=94043&sensor=false");

foreach (var geometry in xdoc.Descendants("geometry"))
{
    var location = geometry.Element("location");
    var lat = location.Element("lat");
    var lng = location.Element("lng");

    string latitude = lat.Value;
    string longtitude = lng.Value;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
solution1368Author Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.