1.在CodeBehind建立命名空間,for Google map api轉出的JSON資料之用途
namespace GoogleGeocodingAPI
{
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
public class Location
{
public string lat { get; set; }
public string lng { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Geometry
{
public Location location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public bool partial_match { get; set; }
public List<string> types { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
public string status { get; set; }
}
}
2. 建立函式
//=======================================================
//<aummary>緯度經度轉中文地址</summary>
//<param name="latLng"></param>
//=======================================================
private static string latLngToChineseAddress(params string[] latLng)
{
string url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + string.Join(",", latLng) + "&language=zh-TW&sensor=true";
string json = string.Empty;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
using (var response = request.GetResponse())
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
json = sr.ReadToEnd();
}
}
GoogleGeocodingAPI.RootObject rootObj = JsonConvert.DeserializeObject<GoogleGeocodingAPI.RootObject>(json);
return rootObj.results[0].formatted_address;
}
3.呼叫轉換函示
private static string GetAddress(string lat,string lng)
{
string result = string.Empty;
result = latLngToChineseAddress(lat, lng);
return result;
}
沒有留言:
張貼留言