/
MainPage.xaml.cs
603 lines (519 loc) · 25.5 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
using System;
using System.Collections.Generic;
using System.Device.Location;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
using Microsoft.Phone.Controls;
using Wiki;
namespace OurWindowsApp
{
public partial class MainPage : PhoneApplicationPage
{
private static string rootUri = "http://en.wikipedia.org/w/api.php";
private static string rootUri1 = "https://maps.googleapis.com/maps/api/place/search/xml?key=AIzaSyCm5alHOA4sBeOz6J_Q60dvKyy1j2SwYbc";
private static string wikiuri = "http://en.wikipedia.org/w/api.php?format=xml&action=query&prop=revisions&rvprop=content&titles=";
private string search = "";
private GeoCoordinateWatcher watcher;
//string accuracyText = "";
private string[] features = { "airport", "train_station", "bus_station", "gas_station", "hospital" };
private double radius = 10000;
private string clat = "23.194176";
private string clng = "72.628384";//23.194176,72.628384
private string slat, slng, currentcity;//for search
public List<Class2> airPlaces, busPlaces, railPlaces, gasPlaces, hospitalPlaces;
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
panorama.DefaultItem = panorama.Items[0];
}
// Load data for the ViewModel Items
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
StartLocationService(GeoPositionAccuracy.Default);
App.ViewModel.LoadData();
}
}
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
textBox1.Text = "";
}
private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
if (textBox1.Text == "")
textBox1.Text = "Search";
}
private void button_Click_1(object sender, RoutedEventArgs e)
{
//Search button is clicked
if (!(textBox1.Text == "Search"))
{
search = textBox1.Text;
postsetlocation(new Uri("http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=" + textBox1.Text));
/*post(new Uri(rootUri + "?action=opensearch&format=xml&search=" + textBox1.Text));
wikipost(new Uri(wikiuri + textBox1.Text));
post1(new Uri(rootUri1 + "&location=" + slat + "," + slng + "&radius=75000&sensor=false&types=" + features[0]));
post1(new Uri(rootUri1 + "&location=" + slat + "," + slng + "&radius=75000&sensor=false&types=" + features[1]));
post1(new Uri(rootUri1 + "&location=" + slat + "," + slng + "&radius=75000&sensor=false&types=" + features[2]));
post1(new Uri(rootUri1 + "&location=" + slat + "," + slng + "&radius=75000&sensor=false&types=" + features[3]));
post1(new Uri(rootUri1 + "&location=" + slat + "," + slng + "&radius=75000&sensor=false&types=" + features[4]));*/
ProgressBar.Visibility = Visibility.Visible;
textBlock1.Text = "Searching...";
}
else
{
MessageBox.Show("Please enter something to search");
}
}
private void beginSearch(string lat, string lng, string stri)
{
airPlaces = new List<Class2>();
airPlaces.Clear();
railPlaces = new List<Class2>();
railPlaces.Clear();
busPlaces = new List<Class2>();
busPlaces.Clear();
hospitalPlaces = new List<Class2>();
hospitalPlaces.Clear();
gasPlaces = new List<Class2>();
gasPlaces.Clear();
post(new Uri(rootUri + "?action=opensearch&format=xml&search=" + stri));
wikipost(new Uri(wikiuri + stri));
post1(new Uri(rootUri1 + "&location=" + lat + "," + lng + "&radius=" + radius + "&sensor=false&types=" + features[0]));
post1(new Uri(rootUri1 + "&location=" + lat + "," + lng + "&radius=" + radius + "&sensor=false&types=" + features[1]));
post1(new Uri(rootUri1 + "&location=" + lat + "," + lng + "&radius=" + radius + "&sensor=false&types=" + features[2]));
post1(new Uri(rootUri1 + "&location=" + lat + "," + lng + "&radius=" + radius + "&sensor=false&types=" + features[3]));
post1(new Uri(rootUri1 + "&location=" + lat + "," + lng + "&radius=" + radius + "&sensor=false&types=" + features[4]));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//Airport button is clicked
panorama.SlideToPage(4);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
//Railways button clicked
panorama.SlideToPage(5);
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
//Weather button is clicked
panorama.SlideToPage(3);
}
private void Button_Click_4(object sender, RoutedEventArgs e)
{
//Wikipedia info button is clicked
panorama.SlideToPage(2);
}
private void gen_query(object sender, TextChangedEventArgs e)
{
search = textBox1.Text;
}
private void whereami_Click(object sender, RoutedEventArgs e)
{
getCityPost(new Uri("http://maps.googleapis.com/maps/api/geocode/xml?sensor=true&latlng=" + clat + "," + clng));
ProgressBar.Visibility = Visibility.Visible;
textBlock1.Text = "Getting Data...";
}
private void getCityPost(Uri u)
{
HttpWebRequest queryRequest = (HttpWebRequest)WebRequest.Create(u);
queryRequest.Method = "POST";
queryUpdateState qState = new queryUpdateState();
qState.AsyncRequest = queryRequest;
queryRequest.BeginGetResponse(new AsyncCallback(getCityHandleResponse), qState);
}
private void getCityHandleResponse(IAsyncResult result)
{
queryUpdateState qState = (queryUpdateState)result.AsyncState;
HttpWebRequest qRequest = (HttpWebRequest)qState.AsyncRequest;
qState.AsyncResponse = (HttpWebResponse)qRequest.EndGetResponse(result);
Stream streamResult;
try
{
streamResult = qState.AsyncResponse.GetResponseStream();
// load the XML
XDocument xmlquery = XDocument.Load(streamResult);
XElement[] addresses = xmlquery.Descendants("result").Descendants("address_component").ToArray();
foreach (var a in addresses)
{
if (a.Descendants("type").First().Value == "administrative_area_level_2")
{
currentcity = a.Descendants("long_name").First().Value;
break;
}
}
beginSearch(clat, clng, currentcity);
}
catch (FormatException)
{
return;
}
}
private void post(Uri u)
{
HttpWebRequest queryRequest = (HttpWebRequest)WebRequest.Create(u);
queryRequest.Method = "POST";
queryUpdateState qState = new queryUpdateState();
qState.AsyncRequest = queryRequest;
queryRequest.BeginGetResponse(new AsyncCallback(HandleResponse), qState);
}
private void HandleResponse(IAsyncResult result)
{
queryUpdateState qState = (queryUpdateState)result.AsyncState;
HttpWebRequest qRequest = (HttpWebRequest)qState.AsyncRequest;
qState.AsyncResponse = (HttpWebResponse)qRequest.EndGetResponse(result);
Stream streamResult;
try
{
streamResult = qState.AsyncResponse.GetResponseStream();
// load the XML
XDocument xmlquery = XDocument.Load(streamResult);
string x = xmlquery.ToString();
if (x.IndexOf("<Description xml:space=\"preserve\">") != -1)
{
x = x.Substring(x.IndexOf("<Description xml:space=\"preserve\">") + 34);
x = x.Substring(0, x.IndexOf("</Description>"));
this.Dispatcher.BeginInvoke(new Action(() => textBlock1.Text = x));
}
else
{
this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Oy")));
}
this.Dispatcher.BeginInvoke(new Action(() => this.ProgressBar.Visibility = Visibility.Collapsed));
}
catch (FormatException)
{
return;
}
}
private void post1(Uri u)
{
HttpWebRequest queryRequest = (HttpWebRequest)WebRequest.Create(u);
queryRequest.Method = "POST";
queryUpdateState qState = new queryUpdateState();
qState.AsyncRequest = queryRequest;
queryRequest.BeginGetResponse(new AsyncCallback(HandleResponse1), qState);
}
private void HandleResponse1(IAsyncResult result)
{
queryUpdateState qState = (queryUpdateState)result.AsyncState;
HttpWebRequest qRequest = (HttpWebRequest)qState.AsyncRequest;
qState.AsyncResponse = (HttpWebResponse)qRequest.EndGetResponse(result);
Stream streamResult;
try
{
streamResult = qState.AsyncResponse.GetResponseStream();
// load the XML
XDocument xmlquery = XDocument.Load(streamResult);
XElement[] a = xmlquery.Descendants("result").Descendants("name").ToArray();
XElement[] geo_lat = xmlquery.Descendants("result").Descendants("geometry").Descendants("location").Descendants("lat").ToArray();
XElement[] geo_lng = xmlquery.Descendants("result").Descendants("geometry").Descendants("location").Descendants("lng").ToArray();
string s = xmlquery.Descendants("result").Descendants("type").ToArray().First().Value;
double[] distance = new double[100];
string[] names = new string[100];
string i = "";
for (int il = 0; il < a.Length; il++)
{
double a1 = (Math.Sin(Radians(double.Parse(clat) - double.Parse(geo_lat[il].Value)) / 2) * Math.Sin(Radians(double.Parse(clat) - double.Parse(geo_lat[il].Value)) / 2)) + Math.Cos(Radians(double.Parse(geo_lat[il].Value))) * Math.Cos(Radians(double.Parse(clat))) * (Math.Sin(Radians(double.Parse(clng) - double.Parse(geo_lng[il].Value)) / 2) * Math.Sin(Radians(double.Parse(clng) - double.Parse(geo_lng[il].Value)) / 2));
double angle = 2 * Math.Atan2(Math.Sqrt(a1), Math.Sqrt(1 - a1));
double Distance = angle * 6378.16;
distance[il] = Math.Round(Distance, 2);
names[il] = a[il].Value;
i += Math.Round(Distance, 2) + " KM away,";
i += a[il].Value + System.Environment.NewLine;
if (s == "airport")
{
if (distance[il] != 0.0 && names[il] != null)
airPlaces.Add(new Class2(names[il], distance[il] + "Kms"));
}
else if (s == "train_station")
{
if (distance[il] != 0.0 && names[il] != null)
railPlaces.Add(new Class2(names[il], distance[il] + "Kms"));
}
else if (s == "bus_station")
{
if (distance[il] != 0.0 && names[il] != null)
busPlaces.Add(new Class2(names[il], distance[il] + "Kms"));
}
else if (s == "hospital")
{
if (distance[il] != 0.0 && names[il] != null)
hospitalPlaces.Add(new Class2(names[il], distance[il] + "Kms"));
}
else if (s == "gas_station")
{
if (distance[il] != 0.0 && names[il] != null)
gasPlaces.Add(new Class2(names[il], distance[il] + "Kms"));
}
}
if (s == "airport")
this.Dispatcher.BeginInvoke(new Action(() => AirportList.ItemsSource = airPlaces));
else if (s == "train_station")
this.Dispatcher.BeginInvoke(new Action(() => RailwaysList.ItemsSource = railPlaces));
else if (s == "bus_station")
this.Dispatcher.BeginInvoke(new Action(() => BusList.ItemsSource = busPlaces));
else if (s == "hospital")
this.Dispatcher.BeginInvoke(new Action(() => HospitalList.ItemsSource = hospitalPlaces));
else if (s == "gas_station")
this.Dispatcher.BeginInvoke(new Action(() => GasList.ItemsSource = gasPlaces));
if (watcher != null)
{
watcher.Stop();
}
}
catch (FormatException)
{
return;
}
}
private void postsetlocation(Uri u)
{
HttpWebRequest queryRequest = (HttpWebRequest)WebRequest.Create(u);
queryRequest.Method = "POST";
queryUpdateState qState = new queryUpdateState();
qState.AsyncRequest = queryRequest;
queryRequest.BeginGetResponse(new AsyncCallback(locHandleResponse), qState);
}
private void locHandleResponse(IAsyncResult result)
{
queryUpdateState qState = (queryUpdateState)result.AsyncState;
HttpWebRequest qRequest = (HttpWebRequest)qState.AsyncRequest;
qState.AsyncResponse = (HttpWebResponse)qRequest.EndGetResponse(result);
Stream streamResult;
try
{
streamResult = qState.AsyncResponse.GetResponseStream();
// load the XML
XDocument xmlquery = XDocument.Load(streamResult);
slat = xmlquery.Descendants("result").Descendants("geometry").Descendants("location").Descendants("lat").First().Value;
slng = xmlquery.Descendants("result").Descendants("geometry").Descendants("location").Descendants("lng").First().Value;
beginSearch(slat, slng, search);
}
catch (FormatException)
{
return;
}
}
private void wikipost(Uri u)
{
HttpWebRequest queryRequest = (HttpWebRequest)WebRequest.Create(u);
queryRequest.Method = "POST";
queryUpdateState qState = new queryUpdateState();
qState.AsyncRequest = queryRequest;
queryRequest.BeginGetResponse(new AsyncCallback(wikiHandleResponse), qState);
}
private void wikiHandleResponse(IAsyncResult result)
{
queryUpdateState qState = (queryUpdateState)result.AsyncState;
HttpWebRequest qRequest = (HttpWebRequest)qState.AsyncRequest;
qState.AsyncResponse = (HttpWebResponse)qRequest.EndGetResponse(result);
Stream streamResult;
try
{
streamResult = qState.AsyncResponse.GetResponseStream();
// load the XML
XDocument xmlquery = XDocument.Load(streamResult);
//Wikipedia Data Need to be parsed
string a = xmlquery.Descendants("query").Descendants("pages").Descendants("page").Descendants("revisions").Descendants("rev").First().Value;
string h = new CreoleParser().ToHTML(a);
//this.Dispatcher.BeginInvoke(new Action(() => webBrowser1.UpdateLayout()));
this.Dispatcher.BeginInvoke(new Action(() => webBrowser1.NavigateToString(h)));
this.Dispatcher.BeginInvoke(new Action(() => webBrowser1.UpdateLayout()));
//string x = xmlquery.ToString();
/* if (x.IndexOf("<Description xml:space=\"preserve\">") != -1)
{
x = x.Substring(x.IndexOf("<Description xml:space=\"preserve\">") + 34);
x = x.Substring(0, x.IndexOf("</Description>"));
this.Dispatcher.BeginInvoke(new Action(() => textBlock1.Text = x));
}
else
{
this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Oy")));
}
this.Dispatcher.BeginInvoke(new Action(() => this.ProgressBar.Visibility = Visibility.Collapsed));*/
}
catch (FormatException)
{
return;
}
}
private double Radians(double p)
{
return p * Math.PI / 180;
}
private void Button_Click_5(object sender, RoutedEventArgs e)
{
//Bus Stop Button is clicked
panorama.SlideToPage(6);
}
/// <summary>
/// Helper method to start up the location data acquisition
/// </summary>
/// <param name="accuracy">The accuracy level </param>
private void StartLocationService(GeoPositionAccuracy accuracy)
{
// Reinitialize the GeoCoordinateWatcher
watcher = new GeoCoordinateWatcher(accuracy);
watcher.MovementThreshold = 20;
// Add event handlers for StatusChanged and PositionChanged events
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
// Start data acquisition
watcher.Start();
}
/// <summary>
/// Handler for the StatusChanged event. This invokes MyStatusChanged on the UI thread and
/// passes the GeoPositionStatusChangedEventArgs
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MyStatusChanged(e));
}
/// <summary>
/// Custom method called from the StatusChanged event handler
/// </summary>
/// <param name="e"></param>
private void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
// The location service is disabled or unsupported.
// Alert the user
// StatusTextBlock.Text = "location is unsupported on this device";
break;
case GeoPositionStatus.Initializing:
// The location service is initializing.
// Disable the Start Location button
// StatusTextBlock.Text = "initializing location service," + accuracyText;
break;
case GeoPositionStatus.NoData:
// The location service is working, but it cannot get location data
// Alert the user and enable the Stop Location button
// StatusTextBlock.Text = "data unavailable," + accuracyText;
break;
case GeoPositionStatus.Ready:
// The location service is working and is receiving location data
// Show the current position and enable the Stop Location button
// StatusTextBlock.Text = "receiving data, " + accuracyText;
break;
}
}
/// <summary>
/// Handler for the PositionChanged event. This invokes MyStatusChanged on the UI thread and
/// passes the GeoPositionStatusChangedEventArgs
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
}
/// <summary>
/// Custom method called from the PositionChanged event handler
/// </summary>
/// <param name="e"></param>
private void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
// Update the TextBlocks to show the current location
clat = e.Position.Location.Latitude.ToString("0.000");
clng = e.Position.Location.Longitude.ToString("0.000");
}
private void Button_Click_6(object sender, RoutedEventArgs e)
{
//Gas Station is clicked
panorama.SlideToPage(8);
}
private void Button_Click_7(object sender, RoutedEventArgs e)
{
//Hospital is clicked
panorama.SlideToPage(7);
}
/* void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
Stream stream = e.Result;
XDocument xDoc = XDocument.Load(stream);
//pivWeather.SelectedIndex = 1;
GetCurrentWeather(xDoc);
GetForeCast(xDoc);
}
private void GetCurrentWeather(XDocument xDoc)
{
try
{
List<WeatherReport> lstWeatherReport = new List<WeatherReport>();
string city = xDoc.Descendants("forecast_information").First().Element("city").Attribute("data").Value;
string url = "http://www.google.com" + xDoc.Descendants("current_conditions").First().Element("icon").Attribute("data").Value;
lstWeatherReport = (from item in xDoc.Descendants("current_conditions")
select new WeatherReport()
{
DayOfWeek = "Today",
City = city,
//WeatherImage = img,
Condition = item.Element("condition").Attribute("data").Value,
//TempF = item.Element("temp_f").Attribute("data").Value,
TempC = item.Element("temp_c").Attribute("data").Value + " C",
Humidity = item.Element("humidity").Attribute("data").Value,
//ImageURL = item.Element("icon").Attribute("data").Value,
Wind = item.Element("wind_condition").Attribute("data").Value
}).ToList();
// lstWeather.ItemsSource = lstWeatherReport;
listBox1.ItemsSource = lstWeatherReport;
}
catch
{
//lstWeather.ItemsSource = null;
listBox1.ItemsSource = null;
}
}
private void GetForeCast(XDocument xDoc)
{
try
{
List<WeatherReport> lstWeatherReport = new List<WeatherReport>();
string city = xDoc.Descendants("forecast_information").First().Element("city").Attribute("data").Value;
lstWeatherReport = (from item in xDoc.Descendants("forecast_conditions")
select new WeatherReport()
{
DayOfWeek = item.Element("day_of_week").Attribute("data").Value,
City = city,
//WeatherImage = img,
Condition = item.Element("condition").Attribute("data").Value,
TempF = item.Element("low").Attribute("data").Value + " F",
TempC = item.Element("high").Attribute("data").Value + " F"
}).ToList();
listBox1.ItemsSource = lstWeatherReport;
}
catch
{
listBox1.ItemsSource = null;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (textBox2.Text.Trim() == string.Empty)
return;
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("http://www.google.com/ig/api?weather=" + textBox2.Text));
}*/
}
public class queryUpdateState
{
public HttpWebRequest AsyncRequest { get; set; }
public HttpWebResponse AsyncResponse { get; set; }
}
}