Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Android.App;
- using Android.Content;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- using Android.Locations;
- using System.Collections.Generic;
- using Android.Util;
- using System.Linq;
- using System.Net;
- using System.IO;
- using System.Threading.Tasks;
- using System.Json;
- namespace Android
- {
- [Activity(Label = "LoyaltyCheckIn", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity, ILocationListener
- {
- Location _currentLocation;
- LocationManager _locationManager;
- string _locationProvider;
- TextView txtLatitude;
- TextView txtLongitude;
- Button btnCheckIn;
- static string FS_client_id = "CTGNYYTIIBK2ZHKR1YIG0L43TDZD2ZTPMONRCPP5W2WA1GCY";
- static string FS_client_secret = "UWPCKE3RE5Y2WM0IB340HYR33IATUHJYVOP5BZHECSQE4QOV";
- static string FS_v = "20140806";
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- txtLatitude = FindViewById<TextView>(Resource.Id.Latitude);
- txtLongitude = FindViewById<TextView>(Resource.Id.Longitude);
- btnCheckIn = FindViewById<Button>(Resource.Id.btnCheckIn);
- btnCheckIn.Click += async (sender, e) =>
- {
- var lat = _currentLocation.Latitude.ToString();
- var lon = _currentLocation.Longitude.ToString();
- 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";
- JsonValue json = await GetNearbyVenues(url);
- DisplayVenues(json);
- };
- InitializeLocationManager();
- }
- protected override void OnResume()
- {
- base.OnResume();
- string Provider = LocationManager.GpsProvider;
- if (_locationManager.IsProviderEnabled(Provider))
- {
- _locationManager.RequestLocationUpdates(Provider, 2000, 1, this);
- }
- else
- {
- }
- }
- protected override void OnPause()
- {
- base.OnPause();
- _locationManager.RemoveUpdates(this);
- }
- void InitializeLocationManager()
- {
- _locationManager = GetSystemService(Context.LocationService) as LocationManager;
- Criteria locationCriteria = new Criteria();
- locationCriteria.Accuracy = Accuracy.Coarse;
- locationCriteria.PowerRequirement = Power.Medium;
- _locationProvider = _locationManager.GetBestProvider(locationCriteria, true);
- if (_locationProvider != null)
- {
- _locationManager.RequestLocationUpdates(_locationProvider, 2000, 1, this);
- }
- else
- {
- }
- }
- public void OnLocationChanged(Location location)
- {
- _currentLocation = location;
- }
- public void OnProviderDisabled(string provider) { }
- public void OnProviderEnabled(string provider) { }
- public void OnStatusChanged(string provider, Availability status, Bundle extras) { }
- private async Task<JsonValue> GetNearbyVenues(string url)
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
- request.ContentType = "application/json";
- request.Method = "GET";
- using (WebResponse response = await request.GetResponseAsync())
- {
- using (Stream stream = response.GetResponseStream())
- {
- JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
- return jsonDoc;
- }
- }
- }
- private void DisplayVenues(JsonValue data)
- {
- for (int i = 0; i < data["response"]["groups"][0]["items"].Count; i++)
- {
- var venueId = data["response"]["groups"][0]["items"][i]["venue"]["id"];
- var venueName = data["response"]["groups"][0]["items"][i]["venue"]["name"];
- var categoryId = data["response"]["groups"][0]["items"][i]["categories"][0]["id"];
- var categoryName = data["response"]["groups"][0]["items"][i]["categories"][0]["name"];
- //TODO: Add clickable label here
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement