Thursday, February 12, 2009

Re: Get Longitude and Latitude from an Address sample, C# asp.net 2.0,3.5


using

System;

using

System.Data;

using

System.Configuration;

using

System.Collections;

using

System.Web;

using

System.Web.Security;

using

System.Web.UI;

using

System.Web.UI.WebControls;

using

System.Web.UI.WebControls.WebParts;

using

System.Web.UI.HtmlControls;

using

System.Xml;

public

partial class Default2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string street = "1600 Pennsylvania Ave";

string city = "Washington";

string state = "DC";

//string zip = "20500";

string geocoderUri = string.Format("http://rpc.geocoder.us/service/rest?address={0},{1},{2}", street, city, state);

XmlDocument geocoderXmlDoc = new XmlDocument();

geocoderXmlDoc.Load(geocoderUri);

XmlNamespaceManager nsMgr = new XmlNamespaceManager(geocoderXmlDoc.NameTable);

nsMgr.AddNamespace(

"geo", @"http://www.w3.org/2003/01/geo/wgs84_pos#");

string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geo:long", nsMgr).InnerText;

string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geo:lat", nsMgr).InnerText;

double latitude = Double.Parse(sLat);

double longitude = Double.Parse(sLong);

Response.Write(

"Latitude: " + latitude + "<br /> Longitude: " + longitude);

}

}


3 comments:

NJ said...

Hi

I got the below error :

Data at the root level is invalid. Line 1, position 1.

At this point

geocoderXmlDoc.Load(geocoderUri);

Can you help me

My Mail address is nj.nikhiljain@gmail.com

Rajeev said...

I think you have to add google dll in bin folder

suma said...

which Dll has to be included.... plzz reply