protected override async void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); var locationClient = LocationServices.GetFusedLocationProviderClient(this); var geofencingClient = LocationServices.GetGeofencingClient(this); var lastLocation = await locationClient.GetLastLocationAsync(); var geoFence = new GeofenceBuilder() .SetRequestId("fence") .SetCircularRegion(lastLocation.Latitude, lastLocation.Longitude, 100) .SetTransitionTypes(Geofence.GeofenceTransitionExit | Geofence.GeofenceTransitionEnter) .SetExpirationDuration(Geofence.NeverExpire) .Build(); var geofenceRequest = new GeofencingRequest.Builder() .SetInitialTrigger(GeofencingRequest.InitialTriggerEnter) .AddGeofence(geoFence) .Build(); var geoIntent = new Intent(this, typeof(GeofenceTransitionsService)); var pendingGeoIntent = PendingIntent.GetService(this, 0, geoIntent, PendingIntentFlags.UpdateCurrent); geofencingClient.AddGeofences(geofenceRequest, pendingGeoIntent); }
private async void SetGeofence() { var locationClient = LocationServices.GetFusedLocationProviderClient(this); var geofencingClient = LocationServices.GetGeofencingClient(this); var lastLocation = await locationClient.GetLastLocationAsync(); var geoFence = new GeofenceBuilder() .SetRequestId("fence") .SetCircularRegion(lastLocation.Latitude, lastLocation.Longitude, 100) .SetTransitionTypes(Geofence.GeofenceTransitionExit | Geofence.GeofenceTransitionEnter) .SetExpirationDuration(Geofence.NeverExpire) .Build(); var geofenceRequest = new GeofencingRequest.Builder() .AddGeofence(geoFence) .Build(); var geoIntent = new Intent(ApplicationContext, typeof(GeofenceTransitionsService)); var pendingGeoIntent = PendingIntent.GetService(this, 0, geoIntent, PendingIntentFlags.UpdateCurrent); geofencingClient.AddGeofences(geofenceRequest, pendingGeoIntent); }
protected virtual async Task Create(GeofenceRegion region) { var transitions = this.GetTransitions(region); var geofence = new GeofenceBuilder() .SetRequestId(region.Identifier) .SetExpirationDuration(Geofence.NeverExpire) .SetCircularRegion( region.Center.Latitude, region.Center.Longitude, Convert.ToSingle(region.Radius.TotalMeters) ) .SetTransitionTypes(transitions) .Build(); var request = new GeofencingRequest.Builder() .SetInitialTrigger(0) .AddGeofence(geofence) .Build(); await this.client .AddGeofencesAsync( request, this.GetPendingIntent() ) .ConfigureAwait(false); }
public override async Task StartMonitoring(GeofenceRegion region) { var transitions = this.GetTransitions(region); var geofence = new GeofenceBuilder() .SetRequestId(region.Identifier) .SetExpirationDuration(Geofence.NeverExpire) .SetCircularRegion( region.Center.Latitude, region.Center.Longitude, Convert.ToSingle(region.Radius.TotalMeters) ) .SetTransitionTypes(transitions) .Build(); var request = new GeofencingRequest.Builder() .AddGeofence(geofence) .SetInitialTrigger(transitions) .Build(); await this.client.AddGeofences( request, this.GetPendingIntent() ); await this.Repository.Set(region.Identifier, region); }
public async void StartMonitoring(GeofenceRegion region) { var geofence = new GeofenceBuilder() .SetRequestId(region.Identifier) .SetExpirationDuration(Geofence.NeverExpire) .SetCircularRegion( region.Center.Latitude, region.Center.Longitude, Convert.ToSingle(region.Radius.TotalMeters) ) .SetTransitionTypes( Geofence.GeofenceTransitionEnter | Geofence.GeofenceTransitionExit ) .Build(); var request = new GeofencingRequest.Builder() .AddGeofence(geofence) .SetInitialTrigger(GeofencingRequest.InitialTriggerEnter | GeofencingRequest.InitialTriggerExit) .Build(); await client.AddGeofencesAsync(request, GetPendingIntent()); lock (syncLock) { regions.Add(region); MarcelloDatabase.Current.Save(region); //if (regions.Count == 1) // Application.Context.StartForegroundService(new Intent(Application.Context, typeof(GeofenceBroadcastReceiver))); } }
public LocationAlarmModel(IRepository <Alarm> repository, IGeofenceService geofenceService, BackgroundTaskManager <GeofenceTask> backgroundTaskManager, GeofenceBuilder builder) { _repository = repository; _geofenceService = geofenceService; _backgroundTaskManager = backgroundTaskManager; _builder = builder; _backgroundTaskManager.TaskCompleted += BackgroundTaskManagerOnTaskCompleted; GeolocationAlarms = new ObservableCollection <Alarm>(); }
private void OnConnected() { if (ContextCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.AccessFineLocation) != Permission.Granted) { ActivityCompat.RequestPermissions(this, new[] { Manifest.Permission.AccessFineLocation }, 5); // MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an // app-defined int constant. The callback method gets the // result of the request. } if (ContextCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.AccessMockLocation) != Permission.Granted) { ActivityCompat.RequestPermissions(this, new[] { Manifest.Permission.AccessMockLocation }, 6); // MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an // app-defined int constant. The callback method gets the // result of the request. } var requestId = "rid"; m_geofencingClient = LocationServices.GeofencingApi; var geoFence = new GeofenceBuilder() .SetRequestId(requestId) .SetTransitionTypes(Geofence.GeofenceTransitionEnter | Geofence.GeofenceTransitionExit | Geofence.GeofenceTransitionDwell) .SetLoiteringDelay(1000) .SetCircularRegion(37.621313, -122.378955, 1000) .SetExpirationDuration(Geofence.NeverExpire) .SetNotificationResponsiveness(1000) .Build(); Intent intent = new Intent(ApplicationContext, typeof(GeoIntentService)); PendingIntent pendingIntent = PendingIntent.GetService(ApplicationContext, 0, intent, PendingIntentFlags.UpdateCurrent); try { m_geofencingClient.AddGeofences(m_googleApiClient, GetAddGeofencingRequest(geoFence), pendingIntent); } catch (IllegalStateException exception) { Log.Error("tag", exception.ToString()); } catch (Exception exception) { } }
public void Monitor(Location location) { var geofence = new GeofenceBuilder() .SetRequestId(location.Id.ToString()) .SetCircularRegion(location.lat, location.lon, (float)location.Radius) .SetTransitionTypes(Geofence.GeofenceTransitionEnter) .SetExpirationDuration(Geofence.NeverExpire) .Build(); var request = new GeofencingRequest.Builder() .AddGeofence(geofence) .Build(); LocationServices.GeofencingApi.AddGeofences(client, request, PendingIntent.GetService(Application.Context, 0, new Intent(Application.Context, new GeofenceService().Class), PendingIntentFlags.UpdateCurrent)); }
public TaskCompletionSource <string> ProcessGeofence() { completionSource = new TaskCompletionSource <string>(); GeofenceBuilder builder = new GeofenceBuilder(); builder.SetCircularRegion(37.3309, -121.8939, 2000); builder.SetExpirationDuration(60 * 60 * 100); //one hour builder.SetRequestId("SanJose Downtown"); builder.SetLoiteringDelay(10000); builder.SetTransitionTypes(Geofence.GeofenceTransitionEnter); builder.SetExpirationDuration(50000); builder.SetNotificationResponsiveness(100); ACPPlaces.ProcessGeofence(builder.Build(), 1); //1 is Geofence Enter Transition. completionSource.SetResult("Geofence Processing Completed"); return(completionSource); }
private void AddProximityAlert(double latitude, double longitude, string poiName, Action <int> enterWork = null, Action <int> exitWork = null) { #region SETUP CODE FOR RECEIVER //NOTE: this hash region does not actually build the geofence it is just setup code. //register enter and exit actions //this is the method that talks to the GeofenceTriggerReceiver receiver.RegisterActions(requestCode, enterWork, exitWork); //creating the receiver Bundle extras = new Bundle(); extras.PutString("name", poiName); extras.PutInt("id", requestCode); Intent intent = new Intent(GeofenceTriggerReceiver.IntentName); intent.PutExtra(GeofenceTriggerReceiver.IntentName, extras); var pendingIntent = PendingIntent.GetBroadcast(this, requestCode, intent, PendingIntentFlags.CancelCurrent); #endregion #region building the geofence if (apiClient != null) { IGeofence geofence = new GeofenceBuilder() .SetTransitionTypes(Geofence.GeofenceTransitionEnter | Geofence.GeofenceTransitionExit) .SetCircularRegion(latitude, longitude, 50) .SetRequestId(requestCode.ToString()) .SetExpirationDuration(Geofence.NeverExpire) .Build(); GeofencingRequest request = new GeofencingRequest.Builder() .AddGeofence(geofence) .Build(); LocationServices.GeofencingApi.AddGeofences(apiClient, request, pendingIntent); } activeGeofences.Add(requestCode, pendingIntent); //requestcode increases as geofence locations increase requestCode++; #endregion }
public async void SetGeofenceTrigger(double lat, double lng, double radius, string entryMessage, string exitMessage, string name) { var builder = new GeofenceBuilder(); var instance = builder.SetRequestId(name) .SetCircularRegion(lat, lng, (float)radius) .SetExpirationDuration(Geofence.NeverExpire) .SetTransitionTypes(Geofence.GeofenceTransitionEnter | Geofence.GeofenceTransitionExit) .Build(); _regions.Add(instance); var request = CreateGeofencingRequest(instance); var pendingIntent = CreatePendintIntent(entryMessage, exitMessage); await _geofenceClient.AddGeofencesAsync(request, pendingIntent); }
public void OnConnected(Bundle connectionHint) { var geofence = new GeofenceBuilder () .SetCircularRegion (42.3633025622483, -71.0857580911102, 100) .SetExpirationDuration (Geofence.NeverExpire) .SetRequestId ("org.neteril.Cytotem") .SetTransitionTypes (Geofence.GeofenceTransitionEnter) .SetNotificationResponsiveness ((int)TimeSpan.FromMinutes (10).TotalMilliseconds) .Build (); var request = new GeofencingRequest.Builder () .AddGeofence (geofence) .SetInitialTrigger (GeofencingRequest.InitialTriggerEnter) .Build (); var intent = new Intent (this, typeof(GeofenceService)); var pendingIntent = PendingIntent.GetService (this, 0, intent, PendingIntentFlags.UpdateCurrent); LocationServices.GeofencingApi.AddGeofences (client, request, pendingIntent); Android.Util.Log.Info ("GeofenceApi", "Successfully added geofence"); }
public void SetGeofences(List <Position> targets, Position currentPosition) { List <Position> nearTargets = GetCloseTargets(targets, currentPosition); List <IGeofence> geofences = new List <IGeofence>(); foreach (Position item in nearTargets) { IGeofence geofence = new GeofenceBuilder() .SetRequestId($"{item.Latitude}_{item.Longitude}") .SetCircularRegion(item.Latitude, item.Longitude, 1000) .SetLoiteringDelay(60 * 1000) .SetTransitionTypes(Geofence.GeofenceTransitionEnter | Geofence.GeofenceTransitionExit | Geofence.GeofenceTransitionDwell) .SetExpirationDuration((long)365 * 24 * 60 * 60 * 1000) .Build(); geofences.Add(geofence); } GeofencingRequest geofencingRequest = new GeofencingRequest.Builder() .AddGeofences(geofences) .Build(); geofencingClient.AddGeofences(geofencingRequest, GetGeofencePendingIntent()); }
public void AddGeofences(GeofenceModel geofenceModel) { //Set parameters geofenceModel.Id = Guid.NewGuid().ToString(); if (geofenceModel.Conversion == 5) //Expiration value that indicates the geofence should never expire. { geofenceModel.Timeout = Geofence.GeofenceNeverExpire; } else { geofenceModel.Timeout = 10000; } List <IGeofence> geofenceList = new List <IGeofence>(); //Geofence Service GeofenceService geofenceService = LocationServices.GetGeofenceService(activity); PendingIntent pendingIntent = CreatePendingIntent(); GeofenceBuilder somewhereBuilder = new GeofenceBuilder() .SetUniqueId(geofenceModel.Id) .SetValidContinueTime(geofenceModel.Timeout) .SetRoundArea(geofenceModel.LatLng.Latitude, geofenceModel.LatLng.Longitude, geofenceModel.Radius) .SetDwellDelayTime(10000) .SetConversions(geofenceModel.Conversion);; //Create geofence request geofenceList.Add(somewhereBuilder.Build()); GeofenceRequest geofenceRequest = new GeofenceRequest.Builder() .CreateGeofenceList(geofenceList) .Build(); //Register geofence //Task geoTask = geofenceService.CreateGeofenceList(geofenceRequest, pendingIntent); //geoTask.AddOnSuccessListener(new CreateGeoSuccessListener(activity)); //geoTask.AddOnFailureListener(new CreateGeoFailListener(activity)); }