I want to know the accuracy for the every fired proximity alert. Is there any way to get that information? can I use locationManager.getLastKnownLocation()? Is there any way to tell the system not to fire alerts if the accuracy is bad?
I want to know the accuracy for the every fired proximity alert. Is there any way to get that information?
Every update called on your LocationListener is passed a Location object for the new event. This object has methods like getAccuracy() that you can use to obtain that information.
Is there any way to tell the system not to fire alerts if the accuracy is bad?
LocationManager.requestLocationUpdates(), only allows you to control the frequency of updates based on time between or distance between each update. It does not allow you to control getting updates from the manager based on accuracy; you will need to use the information from the update to decide whether or not to keep each report.
Related
I don't understand how geofencing can be more accurate than checking location at regular intervals (https://codelabs.developers.google.com/codelabs/background-location-updates-android-o/index.html?index=..%2F..%2Findex#6) ? Because let say I m at position x and i set a geofence of 250m, as soon as the user get out of this geofence, i store the new position and then i set a new geofence of 250m and etc.
Is this not at the end the same than doing mFusedLocationClient.requestLocationUpdates with a smallestDisplacement of 250m ? I don't understand how geofence will use less battery than mFusedLocationClient.requestLocationUpdates
From the link you posted, I don't believe they mean that geofencing is more accurate than checking at regular intervals with location services. The use case is only for a specific area of interest. It also states that geofencing provides a more performant way to "get these notifications". This is just saying that the api provides notifications when a user enters or exits an area.
Here's a use case:
A user is walking around the city and is within 500 meters of a Starbucks. Starbucks app sends a notification with a deal to the user for a discounted drink when they enter the geofence (within 500 meters). This way, Starbucks doesn't track the user constantly to see if they are close to one of their stores.
I don't understand how geofencing can be more accurate than checking location at regular intervals
Who said that geofencing is more accurate than checking location? Constantly requesting location e.g. at 1Hz pace is super accurate and the only more accurate method would be doing the same at 100Hz.
But you will slaughter the battery in about an hour or so.
Geofences tries to act as much accurate not eating your device battery much. If it works? Go figure.
I'm building an app that should be able to report the users exact location. There is only a need for a single location, i.e. I don't need to track the device continuously.
I want the location to be as accurate as possible, and it's okay to wait a short while for the location to be determined (max 1-2 minutes).
I've been looking at FusedLocationProviderClient.getLastLocation(), but since I want the location to be as accurate and updated as possible it doesn't fit my needs.
So I started looking at using FusedLocationProviderClient.requestLocationUpdates() instead, and it seems like a better choice.
But I'm not sure how to best configure my LocationRequest to get as good accuracy as possible. For instance, would it be better to use setNumUpdates() so that I only receive a single update and use that as my location, or should I receive multiple updates in hopes of getting better accuracy (GPS locking to more satellites for example)? I'm thinking that if I use the second approach, I could look at the value of getAccuracy() from each location update and only keep the one with the highest accuracy. The downside is that if the device is moving and I keep receiving updates for a minute or so, the first location could have the highest accuracy, but since it's a minute old it's not accurate any more.
As stated above, I need just a single highly accurate location and it's okay for the app to wait 1-2 minutes for it if needed. What would be the best approach in this kind of scenario?
First, make sure the accurate location is turned on. look at Settings.Secure.LOCATION_MODE_HIGH_ACCURACY It has a noticeable advantage over only using GPS. Then listen for the location for a while and calculate the result you get to find out the best location. You can also detect if the user is moving if the number differs a lot or by using Activity Recognition API. It shouldn't be very hard to write this calculate function to get the best result.
I'm not sure about this but I really don't think waiting more than a few seconds gives you an advantage. to be sure you can simply alter this time and watch the result.
You might want to use LocationManager. In my experience FusedLocation will indeed appear to lock faster but may not be as accurate overall, or at least for a while. I have an app that also requires pretty accurate coordinates. My default is to use a LocationManager based approach but users can switch to a FusedLocation provider if they want faster locking (like when indoors).
This is a good overview https://developer.android.com/guide/topics/location/strategies
For the provider when requesting updates I'd use LocationManager.GPS_PROVIDER. It will take longer to lock since it will wait for satellites and not use Wifi or other towers. But you said that's OK. Something along these lines
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, YourLocationListener);
Once you start getting location updates via your listener's onLocationChanged() you can start to inspect the location for accuracy, movement from last location change, etc. to try and evaluate if the GPS receiver is still settling in on a location. Once you are satisfied with the quality of the fix you can stop receiving location updates (locationManager.removeUpdates(YourLocationListener)) and then run your logic that needs the location. The link I provided has good info about this too.
I'm writing an application which requires that an accurate GPS location is taken at the moment a particular action is taken - specifically, by law we have to prove that we were at someone's house when we said we were.
I have already implemented a Location Listener to update the location for live-tracking purposes, but I also require an on-demand update from the device's GPS.
Using GetLastKnownLocation does not appear to force the GPS device to update - instead, I am getting the last location received from the onLocationChanged event.
Is there a way to force a device to update and get the current location, instead of either getting the last known location or waiting for another onLocationChanged event to fire?
Try using Thread that runs at a particular interval of time and gets the location data..
So that you can use it later..
Right now, I only know of one method to do this:
- Get last known location
- Have the location manager request location updates
However, I really only need to get the CURRENT coordinates ONCE right when the application is called, but it's not doing what I want.
What's the simplest way to get the current coordinates? Is there something I could call or some code I could use just to get the location RIGHT NOW ?
thanks in advance! I'm still a little new with android development.
What's the simplest way to get the current coordinates?
There is no way to get the current coordinates on demand.
Is there something I could call or some code I could use just to get the location RIGHT NOW ?
No, for three related reasons:
Not all location technologies are low power. GPS, for example, is a serious battery hog. Hence, GPS is not powered on unless something is actively seeking a GPS fix.
Not all location technologies are instantaneous. GPS, for example, takes some number of seconds to get a fix after being powered on.
No location technology is universally available. GPS, for example, may be unavailable because you are in a large building.
I'm using
myLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
to retrieve the current location at the start-up of my application.
As the Android documentation states, this location can be "out-of-date", since the method returns the location when the GPS was used the last time.
How can I actively request the current location from the GPS? I thought about using LocationListener, however that might be a bit of an overkill, since I only need the location once (at the start of my app).
Any ideas?
Your initial intuition is correct - you need to use a LocationListener to request updates. Given that you require only a single position, you can unregister the LocationListener after the first value returns.
In practice though, it's probably wise to listen for a little bit longer. Location Based Services (particularly GPS) have a tendency to 'jump around' a bit when they first get their fix. Your best bet is to listen for a set amount of time, or a set number of updates, or until a certain level of accuracy has been achieved (the Location Listener will return the accuracy of the position returned).