♠ Posted by Unknown in C#,WindowsPhone at 4:04 PM
Kali ini saya akan sedikit share bagaimana parsing data xml dengan "Xml.Linq" untuk mendapat data nama Provinsi, Kota, Sub_Kota dari kordinat longitude dan latitude yang didapat dari GPS.contoh : http://maps.googleapis.com/maps/api/geocode/xml?latlng=-6.1333,106.7500&sensor=true
ini adalah koordinat salah satu daerah Tanah Datar, Sumatera Barat
public partial class MainPage : PhoneApplicationPage
{
// Constructor
private WebClient m_myWebClient;
public MainPage()
{
InitializeComponent();
m_myWebClient = new WebClient();
string url = "http://maps.googleapis.com/maps/api/geocode/xml?latlng=-0.3056,100.3692&sensor=true";
Uri uri = new Uri(url);
m_myWebClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myWebClient_DownloadStringCompleted);
m_myWebClient.AllowReadStreamBuffering = true;
m_myWebClient.DownloadStringAsync(uri);
}
private void myWebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XDocument ListArray = XDocument.Parse(e.Result);
string a = e.Result;
//ListArray.Element("GeocodeResponse").Element("result").Element("address_component").Value;
//var ss = from x in ListArray.Descendants("GeocodeResponse") select x.Attribute("administrative_area_level_1");
string SubCity = ListArray.Descendants("type").FirstOrDefault(x => x.Value == "administrative_area_level_3").Parent.Element("long_name").Value;
string City = ListArray.Descendants("type").FirstOrDefault(x => x.Value == "administrative_area_level_2").Parent.Element("long_name").Value;
string Provincy = ListArray.Descendants("type").FirstOrDefault(x => x.Value == "administrative_area_level_1").Parent.Element("long_name").Value;
}
}
Terima Kasih
:)
0 komentar:
Post a Comment