/// <summary> /// Creates the location request. /// </summary> private void createLocationRequest() { locationRequest = LocationRequest.Create(); //create a new location request locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy) //set the location request priority to high .SetInterval(1000) //set the interval for location updates to every minute .SetFastestInterval(1000); //set the fastest interval for location updates to every second }
public async Task DisplayLocationSettingsRequest() { var activity = Xamarin.Essentials.Platform.CurrentActivity; LocationSettingsResponse locationSettingsResponse; try { var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(2000); locationRequest.SetFastestInterval(1250); var locationSettingsRequestBuilder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); locationSettingsResponse = await LocationServices.GetSettingsClient(activity).CheckLocationSettingsAsync(locationSettingsRequestBuilder.Build()); } catch (ApiException ex) { switch (ex.StatusCode) { case CommonStatusCodes.ResolutionRequired: var resolvable = (ResolvableApiException)ex; resolvable.StartResolutionForResult(activity, 0x1); break; default: break; } } }
//This procedure calls //not sure if im calling this right. //1) is it OK to pass in mainactivity like this? //2) any way to get a callback after they enable? (ie, to reload webview) //3) any way to delay the initial load of the webview until they hit the OK button to load fixity? //https://forums.xamarin.com/discussion/118189/gps-location-enable-in-xamarin-forms //https://stackoverflow.com/questions/33251373/turn-on-location-services-without-navigating-to-settings-page //https://stackoverflow.com/questions/43138788/ask-user-to-turn-on-location/43139125 //https://forums.xamarin.com/discussion/140325/how-to-check-every-time-for-gps-connectivity (code based on this) public async void turnOnGps(MainActivity activity) { try { //MainActivity activity = Xamarin.Forms.Context as MainActivity; GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity) .AddApi(LocationServices.API).Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync( googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(activity, 0); } } catch (Java.Lang.Exception ex) { Xamarin.Forms.DependencyService.Get <IMessage>().LongAlert(ex.Message); //show error } }
private static LocationRequest CreateLocationRequest(MvxLocationOptions options) { // NOTE options.TrackingMode is not supported var request = LocationRequest.Create(); switch (options.Accuracy) { case MvxLocationAccuracy.Fine: request.SetPriority(LocationRequest.PriorityHighAccuracy); break; case MvxLocationAccuracy.Coarse: request.SetPriority(LocationRequest.PriorityBalancedPowerAccuracy); break; default: throw new ArgumentOutOfRangeException(); } request.SetInterval((long)options.TimeBetweenUpdates.TotalMilliseconds); request.SetSmallestDisplacement(options.MovementThresholdInM); return(request); }
public async Task StartListener(GpsRequest?request = null) { if (this.IsListening) { return; } request = request ?? new GpsRequest(); var access = await this.RequestAccess(request); access.Assert(); var nativeRequest = LocationRequest .Create() .SetPriority(GetPriority(request.Priority)) .SetInterval(request.Interval.ToMillis()); if (request.ThrottledInterval != null) { nativeRequest.SetFastestInterval(request.ThrottledInterval.Value.ToMillis()); } await this.client.RequestLocationUpdatesAsync( nativeRequest, this.GetPendingIntent() // used for background - should switch to LocationCallback for foreground ); this.IsListening = true; }
public async void turnOnGps() { try { MainActivity activity = global::Xamarin.Forms.Forms.Context as MainActivity; GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity).AddApi(LocationServices.API).Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync( googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(activity, 0); } var result = await LocationServices.SettingsApi.CheckLocationSettingsAsync(googleApiClient, locationSettingsRequestBuilder.Build()); } catch (Exception ex) { } }
public static LocationRequest ToNative(this GpsRequest request) { var nativeRequest = LocationRequest .Create() .SetInterval(request.Interval.ToMillis()); switch (request.Priority) { case GpsPriority.Low: nativeRequest.SetPriority(LocationRequest.PriorityLowPower); break; case GpsPriority.Highest: nativeRequest.SetPriority(LocationRequest.PriorityHighAccuracy); break; case GpsPriority.Normal: default: nativeRequest.SetPriority(LocationRequest.PriorityBalancedPowerAccuracy); break; } if (request.ThrottledInterval != null) { nativeRequest.SetFastestInterval(request.ThrottledInterval.Value.ToMillis()); } if (request.MinimumDistance != null) { nativeRequest.SetSmallestDisplacement((float)request.MinimumDistance.TotalMeters); } return(nativeRequest); }
private LocationRequest CreateRequest(LocationOptions options) { var request = LocationRequest.Create(); request.SetSmallestDisplacement(options.MovementThreshold); request.SetPriority(GetPriority(options.Accuracy)); request.SetInterval((long)options.ReportInterval.TotalMilliseconds); if (options.FastestInterval.HasValue) { request.SetFastestInterval((long)options.FastestInterval.Value.TotalMilliseconds); } if (options.NumberOfUpdates > 0 && options.NumberOfUpdates < int.MaxValue) { request.SetNumUpdates(options.NumberOfUpdates); } if (options.Duration != TimeSpan.MaxValue) { request.SetExpirationDuration((long)options.Duration.TotalMilliseconds); } return(request); }
public void OnConnected(Bundle connectionHint) { mLocationRequest = LocationRequest.Create(); mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); mLocationRequest.SetInterval(1000); Models.DebugUtil.WriteLine("[Droid] GoogleApiClient > OnConnected"); }
private void InitLocationServices() { _googleApiClient = new GoogleApiClient.Builder(this, this, this).AddApi(LocationServices.API).Build(); _locationRequest = LocationRequest.Create(); _locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); _locationRequest.SetInterval(LocationRequestInterval); _locationRequest.SetFastestInterval(LocationRequestFastestInterval); }
public LocationPresenter(Activity activity) { mLocationClient = new LocationClient(activity, this, this); mLocationRequest = LocationRequest.Create(); mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); mLocationRequest.SetInterval(10 * 1000); mLocationRequest.SetFastestInterval(1000); }
public void OnConnected(Bundle connectionHint) { LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityHighAccuracy) .SetInterval(5000) .SetSmallestDisplacement(100f); LocationServices.FusedLocationApi.RequestLocationUpdates(apiClient, locationRequest, this); }
public void StartLocationProvider() { var locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityHighAccuracy) .SetInterval(15000) .SetFastestInterval(5000); _fusedLocationClient.RequestLocationUpdates(locationRequest, this, Looper.MainLooper); }
public void OnConnected(Bundle connectionHint) { Log.Debug(TAG, "OnConnected"); _locationRequest = LocationRequest.Create(); _locationRequest.SetInterval(1000); _locationRequest.SetFastestInterval(1000); _locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); LocationServices.FusedLocationApi.RequestLocationUpdates(_googleApiClient, _locationRequest, this); }
private bool DisplayLocationSettingsRequest() { bool islocationOn = false; var googleApiClient = new GoogleApiClient.Builder(this).AddApi(LocationServices.API).Build(); googleApiClient.Connect(); var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); var builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); builder.SetAlwaysShow(true); var result = LocationServices.SettingsApi.CheckLocationSettings(googleApiClient, builder.Build()); result.SetResultCallback((LocationSettingsResult callback) => { switch (callback.Status.StatusCode) { case LocationSettingsStatusCodes.Success: { islocationOn = true; //DoStuffWithLocation(); break; } case LocationSettingsStatusCodes.ResolutionRequired: { try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). callback.Status.StartResolutionForResult(this, 100); } catch (IntentSender.SendIntentException e) { } break; } default: { // If all else fails, take the user to the android location settings StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings)); break; } } }); return(islocationOn); }
protected override async Task <bool> EnableDeviceLocationService() { var context = Forms.Context; var activity = (MainActivity)context; var listener = new ActivityResultListener(activity); var googleApiClient = new GoogleApiClient.Builder(activity).AddApi(LocationServices.API).Build(); googleApiClient.Connect(); var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); var builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); builder.SetAlwaysShow(true); var result = LocationServices.SettingsApi.CheckLocationSettings(googleApiClient, builder.Build()); result.SetResultCallback((LocationSettingsResult callback) => { switch (callback.Status.StatusCode) { case LocationSettingsStatusCodes.Success: { break; } case LocationSettingsStatusCodes.ResolutionRequired: { try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). callback.Status.StartResolutionForResult(activity, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { } break; } default: { // If all else fails, take the user to the android location settings activity.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings)); break; } } }); return(await listener.Task); }
private void CreateLocationRequest() { locationRequest = LocationRequest.Create(); locationRequest.SetInterval(UPDATE_INTERVAL); locationRequest.SetFastestInterval(UPDATE_FASTEST_INTERVAL); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetSmallestDisplacement(DISPLACEMENT); locationClient = LocationServices.GetFusedLocationProviderClient(this); locationCallBack = new LocationCallBackHelper(); locationCallBack.CurrentLocation += LocationCallBack_CurrentLocation; }
public LocationService(IMvxAndroidGlobals androidGlobals, IMvxAndroidCurrentTopActivity topActivity) { _androidGlobals = androidGlobals; _topActivity = topActivity; _locationRequest = LocationRequest.Create(); _locationRequest.SetSmallestDisplacement(_displacement); _locationRequest.SetPriority(LocationRequest.PriorityLowPower); this.InitializeGoogleAPI(); }
private static LocationRequest GetLocationRequest() { var request = LocationRequest.Create(); request.SetPriority(LocationRequest.PriorityBalancedPowerAccuracy); request.SetInterval((long)LOCATION_UPDATES_IN_MILLISECONDS); request.SetFastestInterval((long)LOCATION_UPDATES_IN_MILLISECONDS); request.SetSmallestDisplacement(LOCATION_UPDATES_IN_METERS); return(request); }
public async void OpenSettings() { LocationManager LM = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService); if (LM.IsProviderEnabled(LocationManager.GpsProvider) == false) { Context ctx = Android.App.Application.Context; p0 = Platform.CurrentActivity; //----------------------------------------------------------------------------------------------------------------- try { GoogleApiClient googleApiClient = new GoogleApiClient.Builder(ctx) .AddApi(LocationServices.API) .Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityBalancedPowerAccuracy) .SetInterval(interval) .SetFastestInterval(fastestInterval); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync(googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(p0, 0); } } catch (Exception exception) { // Log exception } //----------------------------------------------------------------------------------------------------------------- //ctx.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings).SetFlags(ActivityFlags.NewTask)); //Application.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings)); } else { //this is handled in the PCL } }
public LocationReporter(Context context, int userId) { mLocationClient = new LocationClient(context, this, this); mLocationRequest = LocationRequest.Create(); mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); mLocationRequest.SetInterval(10 * 1000); mLocationRequest.SetFastestInterval(1000); this.travelerLocation = new TravelerLocation(); this.travelerLocation.UserId = userId.ToString(); this.context = context; }
public async Task CheckAndTurnOnGPS(Page page) { this.page = page; Int64 interval = 1000 * 60 * 1, fastestInterval = 1000 * 50; try { GoogleApiClient googleApiClient = new GoogleApiClient.Builder(MainActivity.Instance) .AddApi(LocationServices.API) .Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityBalancedPowerAccuracy) .SetInterval(interval) .SetFastestInterval(fastestInterval); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync( googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(MainActivity.Instance, 0); } } catch (Exception exception) { Console.Write(exception.Message); } /*(if ((int)Build.VERSION.SdkInt < 23) * { * await MainActivity.Instance.GetLocationAsync(); * return; * } * * await MainActivity.Instance.GetLocationPermissionAsync(this);*/ }
public LocationService() { GoogleApiClient = new GoogleApiClient.Builder(Android.App.Application.Context) .AddApi(LocationServices.API) .Build(); GoogleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityHighAccuracy) .SetInterval(100000) .SetFastestInterval(100000 / 2); Builder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest) .SetAlwaysShow(true); }
public async Task <bool> TurnOnLocationSettings() { try { Cts = new TaskCompletionSource <bool>(); var client = new GoogleApiClient.Builder(CrossCurrentActivity.Current.Activity).AddApi(LocationServices.API).Build(); client.Connect(); var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityBalancedPowerAccuracy); locationRequest.SetInterval(30 * 1000); locationRequest.SetFastestInterval(5 * 1000); var builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); //builder.SetAlwaysShow(true); //CrossCurrentActivity.Current.ActivityStateChanged += (sender, args) => //{}; var result = await LocationServices.SettingsApi.CheckLocationSettingsAsync(client, builder.Build()); if (result.Status.StatusCode == CommonStatusCodes.Success) { return(true); } if (result.Status.StatusCode == CommonStatusCodes.ResolutionRequired) { result.Status.StartResolutionForResult(CrossCurrentActivity.Current.Activity, 0x1); //REQUEST_CHECK_SETTINGS } else { var settingIntent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings); CrossCurrentActivity.Current.Activity.StartActivity(settingIntent); return(false); } } catch { return(false); } return(await Cts.Task); }
public async static void AskForLocation() { if (!CheckIsGPSEnabled()) { Int64 interval = 1000 * 60 * 1, fastestInterval = 1000 * 50; try { GoogleApiClient googleApiClient = new GoogleApiClient.Builder(MainActivity.AndroidContext) .AddApi(LocationServices.API) .Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityBalancedPowerAccuracy) .SetInterval(interval) .SetFastestInterval(fastestInterval); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync( googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(MainActivity.CurrentActivity, 0); } } catch (Exception) { // Log exception } } }
public void PedirGps() { var googleApiClient = new GoogleApiClient.Builder(Context).AddApi(LocationServices.API).Build(); googleApiClient.Connect(); var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); var builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); builder.SetAlwaysShow(true); var result = LocationServices.SettingsApi.CheckLocationSettings(googleApiClient, builder.Build()); result.SetResultCallback(async(LocationSettingsResult callback) => { switch (callback.Status.StatusCode) { case LocationSettingsStatusCodes.Success: { await Utils.PedirPermissaoLocalizacao(); break; } case LocationSettingsStatusCodes.ResolutionRequired: { try { callback.Status.StartResolutionForResult((Activity)Context, Utils.REQUEST_CHECK_SETTINGS); await Utils.PedirPermissaoLocalizacao(); } catch (IntentSender.SendIntentException) { } break; } } }); }
// ---------------------------------------------------------------------------------------------------------------------------------- // /* This is called when we make a connection to the Google Play servers. * Here is where we want to register our desire to receive location updates. */ public void OnConnected(Bundle connectionHint) { /* create a new LocationRequest object using the LocationRequest.Create builder method. * Set it up to match our usage of location. */ LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityHighAccuracy) .SetInterval(5000) .SetSmallestDisplacement(100f); /* Next, call RequestLocationUpdates on the LocationServices.FusedLocationApi property to setup location updates. * You will need to pass in your API client, the location request and an ILocationListener callback - pass the * activity reference ("this") for that parameter. */ LocationServices.FusedLocationApi.RequestLocationUpdates(apiClient, locationRequest, this); /* Remember that LocationServices uses a different interface for ILocationListener, but the single method * defined - OnLocationChanged, actually matches the original ILocationListener we've already defined. * Since we want to execute the same logic regardless of where the update came from, we can just share the * implementation by adding Android.Gms.Location.ILocationListener as one our activity implements. */ }
public override void OnCreate() { base.OnCreate(); if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService); var notifyChannel = new NotificationChannel( NOTIFY_CHANEL_ID, "FriendLoc", Android.App.NotificationImportance.Max); notificationManager.CreateNotificationChannel(notifyChannel); Notification notification = new NotificationCompat.Builder(this, NOTIFY_CHANEL_ID) .SetContentTitle("") .SetPriority(NotificationCompat.PriorityMax) .SetContentText("Your location is updating on background").Build(); StartForeground(1, notification); } LocationRequest locationRequest = LocationRequest.Create(); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(5000); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); _fusedLocationClient = LocationServices.GetFusedLocationProviderClient(this); _callback = new CusLocationCallback((location) => { ServiceInstances.TripRepository.AddLocation(_tripid, new Common.Models.TripLocation() { Latitude = location.Latitude, Longitude = location.Longitude, TripId = _tripid, UserId = _userId }); }); _fusedLocationClient.RequestLocationUpdates(locationRequest, _callback, Looper.MainLooper); }
protected async override void Work() { try { if (!googleApiClient.IsConnected) { this.OnStart(null); } LocationRequest lr = LocationRequest.Create(); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().AddLocationRequest(lr); builder.SetAlwaysShow(true); var result = await LocationServices.SettingsApi.CheckLocationSettingsAsync(googleApiClient, builder.Build()); var res = LocationServices.FusedLocationApi.GetLastLocation(googleApiClient); if (res != null) { this.genericTCPPacketSender = new GenericTCPPacketSender <ConnectedClientInfoPacket>(this.networkClient); this.genericTCPPacketSender.Send(new ConnectedClientInfoPacket() { ConnectedClientViewModel = new ConnectedClientViewModel() { IsAdmin = false, Latitude = res.Latitude, Longitude = res.Longitude, Machine = CrossDeviceInfo.Current.Model, Name = "Antek" } }); } } catch (FeatureNotEnabledException ex) { throw; } catch (Exception ex) { throw ex; } }
public LocationProvider(Activity context) { Context = context; //apiClient = new GoogleApiClient.Builder (Android.App.Application.Context, this, this).AddApi (LocationServices.API).Build (); apiClient = new GoogleApiClient.Builder(Android.App.Application.Context, this, this).AddApi(LocationServices.API).Build(); locRequest = LocationRequest.Create(); // Setting location priority to PRIORITY_HIGH_ACCURACY (100) locRequest.SetPriority(100); // Setting interval between updates, in milliseconds // NOTE: the default FastestInterval is 1 minute. If you want to receive location updates more than // once a minute, you _must_ also change the FastestInterval to be less than or equal to your Interval locRequest.SetFastestInterval(500); locRequest.SetInterval(1000); WriteLine($"Request priority set to status code {locRequest.Priority}, interval set to {locRequest.Interval} ms"); }