public void SetFence() { bool isWithinRadius = false; foreach (var fence in fences) { float[] results = new float[1]; Location.DistanceBetween(fence.Latitude, fence.Longitude, currentLatitude, currentLongitude, results); float distanceInMeters = results[0]; if (distanceInMeters < radiusInMeters) { isWithinRadius = true; break; } } if (!isWithinRadius) { var intent = new Intent(CustomActions.TODO_WITHIN_PROXIMITY); PendingIntent pendingIntent = PendingIntent.GetService(this, 0, intent, PendingIntentFlags.UpdateCurrent); _locationManager.AddProximityAlert(currentLatitude, currentLongitude, radiusInMeters, -1, pendingIntent); } }
public LocationNotification ScheduleLocationNotification(LocationNotification locationNotification) { try { Intent intent = new Intent(Android.App.Application.Context, typeof(NotificationSender)); intent.PutExtra("ID", locationNotification.Id); intent.PutExtra("Title", locationNotification.Title); intent.PutExtra("Body", locationNotification.Body); intent.PutExtra("Type", "LocationNotification"); var latitude = locationNotification.Position.Latitude; var longitude = locationNotification.Position.Longitude; var radius = locationNotification.Radius; PendingIntent broadcast = PendingIntent.GetBroadcast(Android.App.Application.Context, locationNotification.Id, intent, PendingIntentFlags.UpdateCurrent); LocationManager locationManager = (LocationManager)Android.App.Application.Context.GetSystemService(LocationService); locationManager.AddProximityAlert(latitude, longitude, radius, -1, broadcast); return(locationNotification); } catch (Exception) { return(null); } }
//Add proximity alerts: public void addProximityAlert(double latitude, double longitude, String l) { Intent intent = new Intent(l); PendingIntent proximityIntent = PendingIntent.GetBroadcast(this, 0, intent, 0); locationManager.AddProximityAlert(latitude, longitude, 403, -1, proximityIntent); IntentFilter filter = new IntentFilter(l); RegisterReceiver(new ProximityIntentReceiver(this, l), filter); }
public void AddProximityAlert(String name, double latitude, double longitude, float range) { Log.Info("Vegas", "Adding Location: " + name); Intent intent = new Intent(ACTION_FILTER); intent.PutExtra("Name", name); PendingIntent proximityIntent = PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.UpdateCurrent); locationManager.AddProximityAlert(latitude, longitude, range, 10000, proximityIntent); }
// Get lattitude and longitude of current location // http://developer.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/ protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); locationText = FindViewById <TextView> (Resource.Id.locationText); radiusText = FindViewById <TextView> (Resource.Id.radiusText); distanceText = FindViewById <TextView> (Resource.Id.distanceText); var button = FindViewById <Button> (Resource.Id.button); // Initialize location manager InitializeLocationManager(); locationManager = (LocationManager)GetSystemService(LocationService); // Pass the specified destination and radius to program by clicking button button.Click += (sender, e) => { // Use Geocoder to get longitude and lattitude for destination // http://developer.xamarin.com/recipes/android/os_device_resources/geocoder/geocode_an_address/ var geo = new Geocoder(this); var addresses = geo.GetFromLocationName(locationText.Text, 1); Address endLocation = addresses.ToList().FirstOrDefault(); double endLat = endLocation.Latitude; double endLon = endLocation.Longitude; double radius = Convert.ToDouble(radiusText.Text) * 1609.34; // in meters float radius_meters = (float)radius; destination = new Location(locationProvider); destination.Latitude = endLat; destination.Longitude = endLon; locationManager.RequestLocationUpdates(locationProvider, 0, 0, this); Intent intent = new Intent(PROX_ALERT_INTENT); PendingIntent proximityIntent = PendingIntent.GetBroadcast(this, 0, intent, 0); locationManager.AddProximityAlert(endLat, endLon, radius_meters, -1, proximityIntent); IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); RegisterReceiver(new ProximityIntentReceiver(), filter); /* * * // Calculate the distance between current address and the input address * // http://androidapi.xamarin.com/index.aspx?link=M%3AAndroid.Locations.Location.DistanceBetween(System.Double%2CSystem.Double%2CSystem.Double%2CSystem.Double%2CSystem.Single%5B%5D) * * float[] list = new float[1]; * * Location.DistanceBetween(currentLat, currentLon, endLat, endLon, list); * * // Show distance between current address and destination in the text field * * distance = list[0]/1000; * double distance_round = Math.Round(Convert.ToDouble(distance), 3); * distanceText.Text = distance_round.ToString(); * * // Console.WriteLine ("distance: " + distance); * * // Alert if the distance is smaller than the specified radius * * if (distance_round <= Convert.ToDouble(radiusText.Text)) * { * // if choose screen alert * // alert(sender, e); * * // use notification * // http://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/local_notifications_in_android/ * * // instantiate the builder and set notification elements * Notification.Builder builder = new Notification.Builder(this) * .SetAutoCancel(true) * .SetContentTitle("Alert") * .SetContentText("You are within " + radiusText.Text + " km to your destination") * .SetDefaults(NotificationDefaults.Sound) * .SetSmallIcon(Resource.Drawable.logo); * * // build the notification * Notification notification = builder.Build(); * * // get the notification manager * NotificationManager notificationManager = GetSystemService (Context.NotificationService) as NotificationManager; * * // publish the notification * const int notificationId = 0; * notificationManager.Notify(notificationId, notification); * * } */ }; }