Link to home
Start Free TrialLog in
Avatar of solution1368
solution1368

asked on

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,
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

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.
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
*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

Avatar of solution1368
solution1368

ASKER

but how to capture lat / lng from the xml is my primary question (in asp.net/c#)
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

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?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial