【发布时间】:2013-11-27 11:38:50
【问题描述】:
我有这段代码需要在谷歌地理编码 API 上获取,只有位于“几何”节点中的纬度和经度,但不知道该怎么做,我的教程仅适用于列表,但我只需选择 JSon 中包含的经纬度之一即可。
API JSON:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara",
"short_name" : "Santa Clara",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.42291810,
"lng" : -122.08542120
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.42426708029149,
"lng" : -122.0840722197085
},
"southwest" : {
"lat" : 37.42156911970850,
"lng" : -122.0867701802915
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
namespace Traveler
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
}
// Load data for the ViewModel Items
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
private void btnSearch_ActionIconTapped(object sender, EventArgs e)
{
try
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new System.Uri("http://maps.googleapis.com/maps/api/geocode/json?address=" + txtSearch.Text));
}
catch
{
//criar if para verificar conexão
MessageBox.Show("Try again!");
}
}
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Showing the exact error message is useful for debugging. In a finalized application,
// output a friendly and applicable string to the user instead.
MessageBox.Show(e.Error.Message);
});
}
else
{
// Save the feed into the State property in case the application is tombstoned.
this.State["json"] = e.Result;
ParserJSON(e.Result);
}
}
private List<clsRoute> ParserJSON(string pJSON)
{
//criar um objeto lista do tipo clsRoute
List<clsRoute> lista = new List<clsRoute>();
//Se o JSON está presente
if (pJSON != null)
{
//Faz a conversão (parse) para um tipo jObject
JObject jObj = JObject.Parse(pJSON);
//Le o objeto da lista inteira
JObject jObjectResults = (JObject)jObj["results"];
//Le o objeto da lista results
JArray results = (JArray)jObjectResults["address_components"]["geometry"];
foreach (JObject address_components in results)
{
clsRoute c = new clsRoute()
{
latitude = (Double)address_components["lat"],
longitude = (Double)address_components["lng"]
};
}
//Task here
}
return lista;
}
}
}
如何将纬度和经度添加到 ClsRoute 的 ONE 对象中?
【问题讨论】:
-
是的!但问题是如何读取“位置”节点,但我已经解决了问题,感谢您的时间!
标签: c# json parsing windows-phone-7 latitude-longitude