/
MainPage.xaml.cs
335 lines (289 loc) · 12.3 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
/*
* Copyright © 2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
using BandsinTown;
using BandsinTown.Parser;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Maps.Services;
using Microsoft.Phone.Maps.Toolkit;
using Microsoft.Phone.Shell;
using Nokia.Music;
using Nokia.Music.Tasks;
using Nokia.Music.Types;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Device.Location;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Xml;
using Windows.Devices.Geolocation;
namespace BandsAround
{
/// <summary>
/// Main Page of the application with the Map and Pushpin controls.
/// </summary>
public partial class MainPage : PhoneApplicationPage
{
// Maximum number of Pushpins on the map at the same time
private const int MAX_ARTISTS_COUNT = 2;
// Geolocator for getting device's current location.
private Geolocator geoLocator = null;
IParser iParser = new XMLParserAdapter();
ProgressIndicator _progressIndicator;
/// <summary>
/// Constructor.
/// </summary>
public MainPage()
{
this.InitializeComponent();
_progressIndicator = new ProgressIndicator();
_progressIndicator.IsVisible = true;
_progressIndicator.IsIndeterminate = true;
_progressIndicator.Text = "loading";
SystemTray.SetProgressIndicator(this, _progressIndicator);
this.InitializeMap(this.Map);
this.Loaded += MainPage_Loaded;
GoogleAnalytics.EasyTracker.GetTracker().SendView("MainPage");
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
Map.Layers.Clear();
}
/// <summary>
/// Centers the map on current location at application launch.
/// Subsequently only makes the map visible.
/// </summary>
/// <param name="sender">This page</param>
/// <param name="e">Event arguments</param>
private async void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
// Make Map visible again after returning from about page.
Map.Visibility = Visibility.Visible;
if (Settings.GetValueOrDefault<Boolean>("location", true))
{
//Do Something
if (geoLocator == null)
{
try
{
geoLocator = new Geolocator();
Geoposition pos = await geoLocator.GetGeopositionAsync();
if (pos != null && pos.Coordinate != null)
{
// make pin here
Map.SetView(new GeoCoordinate(pos.Coordinate.Latitude, pos.Coordinate.Longitude), 10);
MapLayer layer1 = new MapLayer();
Pushpin pushpin1 = new Pushpin();
pushpin1.GeoCoordinate = new GeoCoordinate(pos.Coordinate.Latitude, pos.Coordinate.Longitude);
pushpin1.Content = "You are here!";
MapOverlay overlay1 = new MapOverlay();
overlay1.Content = pushpin1;
overlay1.GeoCoordinate = new GeoCoordinate(pos.Coordinate.Latitude, pos.Coordinate.Longitude);
layer1.Add(overlay1);
Map.Layers.Add(layer1);
GetNearbyArtists();
}
}
catch (Exception /*ex*/)
{
// Couldn't get current location. Location may be disabled.
MessageBox.Show("Current location cannot be obtained. It is "
+ "recommended that location service is turned "
+ "on when using Bands in Town.\n"
+ "\nNow centering on London.");
Map.Center = new GeoCoordinate(51.51, -0.12);
Map.SetView(new GeoCoordinate(51.51, -0.12), 10);
GetNearbyArtists();
}
}
}
else
{
MessageBox.Show("Current location cannot be obtained. It is "
+ "recommended that location service is turned "
+ "on when using Bands in Town.\n"
+ "\nNow centering on London.");
Map.Center = new GeoCoordinate(51.51, -0.12);
Map.SetView(new GeoCoordinate(51.51, -0.12), 10);
GetNearbyArtists();
}
}
/// <summary>
/// Open Nokia Music application to show selected artist.
/// </summary>
/// <param name="sender">Selected Pushpin</param>
/// <param name="e">Event arguments</param>
private void pushpintap(object sender, MouseButtonEventArgs e)
{
//Messagebox are what ever
Pushpin cls = sender as Pushpin;
NavigationService.Navigate(new Uri("/EventPage.xaml?msg=" + cls.Tag, UriKind.Relative));
}
/// <summary>
/// Navigates to About page. Hides the map to avoid flickering
/// pushpins while navigating between pages.
/// </summary>
/// <param name="sender">About menu item</param>
/// <param name="e">Event arguments</param>
private void AboutClicked(object sender, EventArgs e)
{
Map.Visibility = Visibility.Collapsed;
NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
}
private void SearchClicked(object sender, EventArgs e)
{
Map.Visibility = Visibility.Collapsed;
NavigationService.Navigate(new Uri("/SearchEvent.xaml", UriKind.Relative));
}
/// <summary>
/// Requests nearby artists after map has been moved to a new position.
/// </summary>
/// <param name="sender">Map</param>
/// <param name="e">Event arguments</param>
private void OnResolveCompleted(object sender, MapResolveCompletedEventArgs e)
{
_progressIndicator.IsVisible = true;
GetNearbyArtists();
}
private void createPinInMap(List<EventSearch> events)
{
// The pushpin to add to the map.
for (int i = 0; i < events.Count; i++)
{
MapLayer pushpin_layer = new MapLayer();
Pushpin pushpin1 = new Pushpin();
pushpin1.GeoCoordinate = new GeoCoordinate(events[i].latitude, events[i].longitude);
pushpin1.Content = events[i].title;
pushpin1.MouseLeftButtonUp += new MouseButtonEventHandler(pushpintap);
pushpin1.Tag = events[i].id;
MapOverlay overlay1 = new MapOverlay();
overlay1.Content = pushpin1;
overlay1.GeoCoordinate = new GeoCoordinate(events[i].latitude, events[i].longitude);
pushpin_layer.Add(overlay1);
Map.Layers.Add(pushpin_layer);
}
_progressIndicator.IsVisible = false;
}
/// <summary>
/// Requests artists and bands originating nearby map center.
/// Each artist/band is represented on the map as Pushpin.
/// </summary>
/// <param name="sender">Refresh menu item</param>
/// <param name="e">Event arguments</param>
private ReverseGeocodeQuery reverseGeocode = null;
private void GetNearbyArtists()
{
try
{
if (reverseGeocode == null || !reverseGeocode.IsBusy)
{
reverseGeocode = new ReverseGeocodeQuery();
reverseGeocode.GeoCoordinate = new GeoCoordinate(Map.Center.Latitude, Map.Center.Longitude);
reverseGeocode.QueryCompleted += reverseGeocode_QueryCompleted;
reverseGeocode.QueryAsync();
}
}
catch (Exception /*ex*/)
{
MessageBox.Show("Something went wrong");
}
}
private async void getLocationData(String cityName)
{
try
{
string when = Settings.GetValueOrDefault<string>("when", "Next+30+days");
string distance = Settings.GetValueOrDefault<string>("distance", "20");
//Map.Layers.Clear();
ObservableCollection<EventSearch> list = await iParser.getEventFeed(new Uri("http://api.eventful.com/rest/events/search?&app_key=G79FxVMQbxjMTKMk&location=" + cityName + "&category=music&within="+ distance +"&date=" + when));
List<EventSearch> newEvents = new List<EventSearch>();
for (int i = 0; i < list.Count; i++)
{
newEvents.Add(list[i]);
}
createPinInMap(newEvents);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
void reverseGeocode_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
{
if (e.Error == null)
{
if (e.Result.Count > 0)
{
MapAddress geoAddress = e.Result[0].Information.Address;
String city = geoAddress.City;
getLocationData(city);
}
}
}
#region Map Helpers
// These helpers enable the Pushpin control from Windows Phone Toolkit
// to be used in representing the artists and bands on the Map control.
private void InitializeMap(Map map)
{
ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(map);
IEnumerable<FieldInfo> runtimeFields = this.GetType().GetRuntimeFields();
foreach (DependencyObject i in children)
{
SetChildItemField(i, runtimeFields);
}
}
private void SetChildItemField(DependencyObject obj, IEnumerable<FieldInfo> fields)
{
var info = obj.GetType().GetProperty("Name");
if (info != null)
{
string name = (string)info.GetValue(obj);
if (name != null)
{
foreach (FieldInfo j in fields)
{
if (j.Name == name)
{
j.SetValue(this, obj);
break;
}
}
}
}
Panel panelItem = obj as Panel;
if (panelItem != null)
{
foreach (UIElement child in panelItem.Children)
{
SetChildItemField(child, fields);
}
}
}
#endregion
private void Map_Loaded_1(object sender, RoutedEventArgs e)
{
Microsoft.Phone.Maps.MapsSettings.ApplicationContext.ApplicationId = "45d6df3b-3921-4cec-926c-b1e4a8450d82";
Microsoft.Phone.Maps.MapsSettings.ApplicationContext.AuthenticationToken = "X_8v2WA-V_HnHKILzFouXg";
}
private void SettingsClicked(object sender, EventArgs e)
{
Map.Visibility = Visibility.Collapsed;
NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative));
}
}
}