从asp.net MVC调用Google Map Web Service API
|
我发现以下代码可以调用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[\"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 个回复
犁攀富