I have this code that I need to get on Google Geocoding API, only the latitude and longitude located in the "Geometry" node, but do not know how to do this, I have the tutorial is only for lists, but I need to pick only one of latitudes and longitudes contained in 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;
}
}
}
How to add the Latitude and Longitude to ONE object of ClsRoute?