void CheckLocationStatus(Object state) { TimerState s = (TimerState)state; //s.counter++; if (locator.IsGeolocationEnabled && locator.IsGeolocationAvailable) { // it's OK now, can stop timer s.tmr.Dispose(); s.tmr = null; //TimeSpan gpsFreq = new TimeSpan(2 * TimeSpan.TicksPerMinute); locator.StopListeningAsync(); // locator.StartListeningAsync(gpsFreq, 0); locator.StartListeningAsync(120000, 0); locator.PositionChanged += Locator_PositionChanged; locator.PositionError += Locator_PositionError; } else { // warn user again //_player.Start(); builder.SetContentTitle("GPS error:"); builder.SetContentText("Trying to restart: " + DateTime.Now.ToShortTimeString()); notification = builder.Build(); notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; notificationID = (int)NotificationFlags.ForegroundService; } }
/* ------------------------------------------------------ */ // --------------------- Buttoms ------------------------ */ /* ------------------------------------------------------ */ async private void ButtonStartClicked(object sender, EventArgs e) { startButton.IsEnabled = false; var nPosition = await map.GetCurrentPosition(locator); if (nPosition == null) { DisplayAlertGPS(); startButton.IsEnabled = true; } else { FinishButton.IsEnabled = true; poopButton.IsEnabled = true; poopButton.Image = "dogPoop"; peeButton.IsEnabled = true; peeButton.Image = "dogPee"; var position = (Position)nPosition; map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(position.Latitude, position.Longitude), distanceFromMapInMiles)); map.RouteCoordinates.Add(new Position(position.Latitude, position.Longitude)); await locator.StartListeningAsync(100, 0.1); locator.PositionChanged += Current_PositionChanged; hasGPS = true; isListening = true; currentDateTime = DateTime.Now; AddStartFlag(); } }
void StartService() { ISharedPreferences prefs = Android.Preferences.PreferenceManager.GetDefaultSharedPreferences(this); try { Location.owner = prefs.GetInt("owner", 0); Location.username = prefs.GetString("user", "unknown"); } catch { Location.owner = 0; } builder = new Notification.Builder(this) .SetSmallIcon(Resource.Drawable.icon)//.setTicker(text).setWhen(time) .SetContentTitle("Location Service is running") .SetContentText("Location Service is running"); //.SetContentIntent(pendingIntent); notification = builder.Build(); notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; notificationID = (int)NotificationFlags.ForegroundService; builder.SetAutoCancel(true); StartForeground(notificationID, notification); savedLocations = new List <Location>(); lastLocation = new Location(); lastLocation.Latitude = 0; lastLocation.Longitude = 0; locator = Plugin.Geolocator.CrossGeolocator.Current; locator.DesiredAccuracy = 50; // TimeSpan updateFreq = new TimeSpan(TimeSpan.TicksPerMinute * 2); // locator.StartListeningAsync(updateFreq, 0); #if TESTING locator.StartListeningAsync(30000, 0); #else // get new location every 2 minutes locator.StartListeningAsync(120000, 0); #endif locator.PositionChanged += Locator_PositionChanged; locator.PositionError += Locator_PositionError; }
void StartService() { // Set up an intent so that tapping the notifications returns to this app: // Intent intent = new Intent(this, typeof(MainApplication)); // Create a PendingIntent; we're only using one PendingIntent (ID = 0): //const int pendingIntentId = 0; //PendingIntent pendingIntent = // PendingIntent.GetActivity(this, pendingIntentId, intent, PendingIntentFlags.OneShot); ISharedPreferences prefs = Android.Preferences.PreferenceManager.GetDefaultSharedPreferences(this); try { Location.owner = prefs.GetInt("owner", 0); } catch { Location.owner = 0; } builder = new NotificationCompat.Builder(this) .SetSmallIcon(Resource.Drawable.ic_stat_directions_bike)//.setTicker(text).setWhen(time) .SetContentTitle("Location Service is running") .SetContentText("Location Service is running") .SetAutoCancel(true) .SetOngoing(false) .SetContentIntent(PendingIntent.GetActivity(this, 0, new Intent(), 0)); notification = builder.Build(); notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; notificationID = (int)NotificationFlags.AutoCancel; StartForeground(notificationID, notification); savedLocations = new List <Location>(); lastLocation = new Location(); lastLocation.Latitude = 0; lastLocation.Longitude = 0; locator = Plugin.Geolocator.CrossGeolocator.Current; //TTS = Plugin.TextToSpeech.CrossTextToSpeech.Current; locator.DesiredAccuracy = 50; //locator.StartListeningAsync(12000, 0); System.TimeSpan updateFreq = new TimeSpan(300000); locator.StartListeningAsync(updateFreq, 0); locator.PositionChanged += Locator_PositionChanged; locator.PositionError += Locator_PositionError; // _player = Android.Media.MediaPlayer.Create(this, Resource.Raw.Alarm); //TTS.Speak("service started"); //binder = new LocationServiceBinder(this); }
public MainPage() { InitializeComponent(); ServicePointManager .ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; locator = CrossGeolocator.Current; locator.StartListeningAsync(TimeSpan.FromSeconds(30), 10.0); locator.PositionChanged += (object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e) => gps_position_changed(sender, e); map.IsEnabled = false; }
async private void InitMap(double speedThresholdKMH) { map.speedThresholdKMH = speedThresholdKMH; isListening = false; hasGPS = false; isPeeClicked = false; isPoopClicked = false; locator = CrossGeolocator.Current; locator.DesiredAccuracy = locatorDesiredAccuracy; map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(31.771959, 34.87018), Distance.FromMiles(50))); distance.Text = "0"; var nPosition = await map.GetCurrentPosition(locator); if (nPosition == null) { DisplayAlertGPS(); } else { var position = (Position)nPosition; map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(position.Latitude, position.Longitude), distanceFromMapInMiles)); } //dealing with GPS that was turned off and then start working TimeSpan t = new TimeSpan(0, 0, 1); Device.StartTimer(t, () => { if (locator.IsGeolocationEnabled == false) { hasGPS = false; } else { if (hasGPS == false && isListening == true) { Device.BeginInvokeOnMainThread(() => { DisplayAlert("GPS", "Retriving GPS location", "OK"); }); locator.StartListeningAsync(100, 0.1); locator.PositionChanged += Current_PositionChanged; } hasGPS = true; } return(true); }); }
private void SetUpEventWidget() { TrackingButton.Text = "Start Location"; TrackingButton.Clicked += async(sender, events) => { if (TrackingButton.Text == "Start Location") { // tracking Geolocator = CrossGeolocator.Current; Geolocator.DesiredAccuracy = 100; Geolocator.PositionChanged += (_sender, _event) => { Console.WriteLine($"Position Status: {_event.Position.Timestamp}"); Console.WriteLine($"Position Latitude: {_event.Position.Latitude}"); Console.WriteLine($"Position Longitude: {_event.Position.Longitude}"); var currentLocation = new Position(_event.Position.Latitude, _event.Position.Longitude); map.MoveToRegion(MapSpan.FromCenterAndRadius(currentLocation, Distance.FromMeters(200))); }; await Geolocator.StartListeningAsync(TimeSpan.FromSeconds(5), 50); Device.BeginInvokeOnMainThread(() => { TrackingButton.Text = "Stop Location"; }); } else { bool result = await DisplayAlert("title", "message", "yes", "no"); if (result == true) { await Geolocator.StopListeningAsync(); Device.BeginInvokeOnMainThread(() => { TrackingButton.Text = "Start Location"; }); } } }; }
protected async override void OnAppearing() { base.OnAppearing(); locator = CrossGeolocator.Current; locator.PositionChanged += Locator_PositionChanged; await locator.StartListeningAsync(TimeSpan.FromSeconds(0), 100); var position = await locator.GetPositionAsync(); var center = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude); var span = new Xamarin.Forms.Maps.MapSpan(center, 1, 1); locationsMap.MoveToRegion(span); using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation)) { conn.CreateTable <Post>(); var posts = conn.Table <Post>().ToList(); DisplayInMap(posts); } }