2011-04-18 73 views
0

我發現下面的代碼來調用谷歌地圖的Web服務API: -調用谷歌地圖的Web服務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["GoogleMapsKey"]; 

string url = 「http://maps.google.com/maps/geo?q={0}&output=xml&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 >= 2) 

{gl = new Geolocation(Convert.ToDecimal(coordinateArray[1].ToString()), 
Convert.ToDecimal(coordinateArray[0].ToString())); 

} 

}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;}} 

**所以我創建了一個包含上述新模式類代碼,並測試我的工作我已經寫在控制器

Geocoder.ResolveAddress(「University road」,」rajkot」,」gujarat」,」",」India」) 

但我仍然不知道如何創建相關的視圖來顯示地圖下面? 在此先感謝您的幫助。**

回答

1

我已經發布了一個名爲「google-maps」包裝庫的開源項目。

有了它,你可以很容易地查詢谷歌地圖的地理編碼,方向,立面,並在未來的地方。

使用此庫,您只能從地理編碼中獲取數據。不包括UI控件。

請在這裏查看 - http://code.google.com/p/google-maps/