Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. using System;
  2. using Android.App;
  3. using Android.Content;
  4. using Android.Runtime;
  5. using Android.Views;
  6. using Android.Widget;
  7. using Android.OS;
  8. using Android.Locations;
  9. using System.Collections.Generic;
  10. using Android.Util;
  11. using System.Linq;
  12. using System.Net;
  13. using System.IO;
  14. using System.Threading.Tasks;
  15. using System.Json;
  16.  
  17. namespace Android
  18. {
  19. [Activity(Label = "LoyaltyCheckIn", MainLauncher = true, Icon = "@drawable/icon")]
  20. public class MainActivity : Activity, ILocationListener
  21. {
  22. Location _currentLocation;
  23. LocationManager _locationManager;
  24.  
  25. string _locationProvider;
  26.  
  27. TextView txtLatitude;
  28. TextView txtLongitude;
  29. Button btnCheckIn;
  30.  
  31. static string FS_client_id = "CTGNYYTIIBK2ZHKR1YIG0L43TDZD2ZTPMONRCPP5W2WA1GCY";
  32. static string FS_client_secret = "UWPCKE3RE5Y2WM0IB340HYR33IATUHJYVOP5BZHECSQE4QOV";
  33. static string FS_v = "20140806";
  34.  
  35. protected override void OnCreate(Bundle bundle)
  36. {
  37. base.OnCreate(bundle);
  38.  
  39. SetContentView(Resource.Layout.Main);
  40.  
  41. txtLatitude = FindViewById<TextView>(Resource.Id.Latitude);
  42. txtLongitude = FindViewById<TextView>(Resource.Id.Longitude);
  43. btnCheckIn = FindViewById<Button>(Resource.Id.btnCheckIn);
  44.  
  45. btnCheckIn.Click += async (sender, e) =>
  46. {
  47. var lat = _currentLocation.Latitude.ToString();
  48. var lon = _currentLocation.Longitude.ToString();
  49. string url = $"https://api.foursquare.com/v2/venues/explore?client_id={FS_client_id}&client_secret={FS_client_secret}&ll={lat},{lon}&v={FS_v}&radius=100";
  50.  
  51. JsonValue json = await GetNearbyVenues(url);
  52.  
  53. DisplayVenues(json);
  54. };
  55.  
  56. InitializeLocationManager();
  57. }
  58.  
  59. protected override void OnResume()
  60. {
  61. base.OnResume();
  62. string Provider = LocationManager.GpsProvider;
  63.  
  64. if (_locationManager.IsProviderEnabled(Provider))
  65. {
  66. _locationManager.RequestLocationUpdates(Provider, 2000, 1, this);
  67. }
  68. else
  69. {
  70. }
  71. }
  72.  
  73. protected override void OnPause()
  74. {
  75. base.OnPause();
  76. _locationManager.RemoveUpdates(this);
  77. }
  78.  
  79. void InitializeLocationManager()
  80. {
  81. _locationManager = GetSystemService(Context.LocationService) as LocationManager;
  82. Criteria locationCriteria = new Criteria();
  83.  
  84. locationCriteria.Accuracy = Accuracy.Coarse;
  85. locationCriteria.PowerRequirement = Power.Medium;
  86.  
  87. _locationProvider = _locationManager.GetBestProvider(locationCriteria, true);
  88.  
  89. if (_locationProvider != null)
  90. {
  91. _locationManager.RequestLocationUpdates(_locationProvider, 2000, 1, this);
  92. }
  93. else
  94. {
  95.  
  96. }
  97. }
  98.  
  99. public void OnLocationChanged(Location location)
  100. {
  101. _currentLocation = location;
  102. }
  103.  
  104. public void OnProviderDisabled(string provider) { }
  105.  
  106. public void OnProviderEnabled(string provider) { }
  107.  
  108. public void OnStatusChanged(string provider, Availability status, Bundle extras) { }
  109.  
  110.  
  111. private async Task<JsonValue> GetNearbyVenues(string url)
  112. {
  113. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
  114. request.ContentType = "application/json";
  115. request.Method = "GET";
  116.  
  117. using (WebResponse response = await request.GetResponseAsync())
  118. {
  119. using (Stream stream = response.GetResponseStream())
  120. {
  121. JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
  122. return jsonDoc;
  123. }
  124. }
  125. }
  126.  
  127. private void DisplayVenues(JsonValue data)
  128. {
  129.  
  130. for (int i = 0; i < data["response"]["groups"][0]["items"].Count; i++)
  131. {
  132. var venueId = data["response"]["groups"][0]["items"][i]["venue"]["id"];
  133. var venueName = data["response"]["groups"][0]["items"][i]["venue"]["name"];
  134. var categoryId = data["response"]["groups"][0]["items"][i]["categories"][0]["id"];
  135. var categoryName = data["response"]["groups"][0]["items"][i]["categories"][0]["name"];
  136.  
  137. //TODO: Add clickable label here
  138. }
  139.  
  140.  
  141. }
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement