Friday, February 10, 2012

Calling Google map web service API using asp.net MVC?

I found the following code to call Google map web service API:-

public static class Geocoder{

private static string _GoogleMapsKey = Config.getAppSetting(“GoogleMapsKey”);

public static Geolocation? ResolveAddress(string query)

{if (string.IsNullOrEmpty(_GoogleMapsKey))

_GoogleMapsKey = ConfigurationManager.AppSettings["Google…

string url = “http://maps.google.com/maps/geo?q={0}%26amp;output=xml%26amp;key=” + _GoogleMapsKey;

url = String.Format(url, query);

XmlNode coords = null;

try{

string xmlString = GetUrl(url);

XmlDocument xd = new XmlDocument();

xd.LoadXml(xmlString);

XmlNamespaceManager xnm = new XmlNamespaceManager(xd.NameTable);

coords = xd.GetElementsByTagName(“coordinates”)[0…

}catch { }

Geolocation? gl = null;

if (coords != null){

string[] coordinateArray = coords.InnerText.Split(‘,’);

if (coordinateArray.Length %26gt;= 2)

{gl = new Geolocation(Convert.ToDecimal(coordinate… Convert.ToDecimal(coordinateArray[0].ToS…

}

}return gl;

}public static Geolocation? ResolveAddress(string address, string city, string state, string postcode, string country)

{return ResolveAddress(address + “,” + city + “,” + state + “,” + postcode + ” “ + country);}

private static string GetUrl(string url)

{string result = string.Empty;

System.Net.WebClient Client = new WebClient();

using (Stream strm = Client.OpenRead(url))

{StreamReader sr = new StreamReader(strm);

result = sr.ReadToEnd();

}return result;}}

public struct Geolocation

{public decimal Lat;

public decimal Lon;

public Geolocation(decimal lat, decimal lon){

Lat = lat;

Lon = lon;}

public override string ToString()

{return “Latitude: “ + Lat.ToString() + ” Longitude: “ + Lon.ToString();

}public string ToQueryString()

{return “+to:” + Lat + “%2B” + Lon;}}



So i created a new model class that contains that above code, and to test my work i have wrote the following in the controller



Geocoder.ResolveAddress(“University road”,”rajkot”,”gujarat”,”",”India”)



But i still do not know how to create the associated view to display the map?

Thanks in advance for any help.Calling Google map web service API using asp.net MVC?
the code is it clear , try to organize it up.

No comments:

Post a Comment