Wednesday, December 21, 2005

Geocoder Web Service Code Snippet for Google Map API AJAX APPS

public class MapData
{
private ArrayList arrMData;
private float lat;
private float lng;
public MapData()
{
}
public void SetGeocodes(string strAddr)
{
try
{
us.geocoder.GeoCode_Service c = new kdwebsite.us.geocoder.GeoCode_Service();
us.geocoder.GeocoderAddressResult[] r = c.geocode_address(strAddr);
foreach (us.geocoder.GeocoderAddressResult code in r)
{
lat = code.lat;
lng = code.@long;
}
}
catch (Exception e)
{
HttpContext.Current.Response.Write(string.Format("

{0}: {1}

", e.Message, strAddr));
return;
}

}
public void InsertXML(ArrayList arrData, string strPath)
{
this.arrMData = arrData;
XmlDocument doc = new XmlDocument();
// Load the file into an XmlDocument object.
try
{
//Call webservices to fill lat and lng.
SetGeocodes(Convert.ToString(arrData[1]));

//start opening xml file.
doc.Load(strPath);

// Create the element node and set the attribute:
XmlNode root = doc.DocumentElement;
XmlNode foundNode = doc.SelectSingleNode("markers");

XmlNode markerNode= doc.CreateNode(XmlNodeType.Element, "marker",string.Empty);
XmlAttribute attrLat = doc.CreateAttribute("lat");
attrLat.InnerText = lat.ToString();
markerNode.Attributes.Append(attrLat);

XmlAttribute attrLng = doc.CreateAttribute("lng");
attrLng.InnerText = lng.ToString();
markerNode.Attributes.Append(attrLng);

XmlAttribute attr0 = doc.CreateAttribute("title");
attr0.InnerText = arrData[0].ToString();
markerNode.Attributes.Append(attr0);

XmlAttribute attr1 = doc.CreateAttribute("address");
attr1.InnerText = arrData[1].ToString();
markerNode.Attributes.Append(attr1);

XmlAttribute attr2 = doc.CreateAttribute("phone");
attr2.InnerText = arrData[2].ToString();
markerNode.Attributes.Append(attr2);

XmlAttribute attr3 = doc.CreateAttribute("fax");
attr3.InnerText = arrData[3].ToString();
markerNode.Attributes.Append(attr3);

XmlAttribute attr4 = doc.CreateAttribute("email");
attr4.InnerText = arrData[4].ToString();
markerNode.Attributes.Append(attr4);

XmlAttribute attr5 = doc.CreateAttribute("website");
attr5.InnerText = arrData[5].ToString();
markerNode.Attributes.Append(attr5);

foundNode.AppendChild(markerNode);
doc.PreserveWhitespace = false;
doc.Save(strPath);
}
catch (Exception e)
{
HttpContext.Current.Response.Write(string.Format("

{0}: {1}

", e.Message, strPath));
return;
}

}
}

0 Comments:

Post a Comment

<< Home