I would like to discuss with everyone, sharing why using location services is not as fast as google-map.
Now, I created two thread location-services:
GoogleApiClient (With LocationServices.API)
Custom own location services with GPS & Network provider
With GoogleAPIClient (LocationServices) I config
PRIORITY_HIGH_ACCURACY
FAST_INTERVAL_FUSED_POSITION (10 sec)
INTERVAL_FUSED_POSITION (60 sec)
With own thread receive location from gps, network with 3 minutes I request get location from gps, network providers.
But sometime My app receive not fast as GoogleMap app. Ex:
When a long time my app can't receive location, open Google Map takes
location right away and re-open My App is ok.
I always noticed google-map gets position in few seconds, and my app
not fast as.
Everyone, who are using location service together discuss, sharing something to improve the speed of locating position.
Try using the Play Services fused location provider and its getLastLocation() method. Documentation is available at https://developer.android.com/training/location/retrieve-current.html.
If location is enabled on your device before your app opens and you use this method, you should get a location fairly quickly.
Related
Our application uses Google Fused location API to receive location updates every 3 minutes.
The location request settings are as follows –
Interval – 3min
Fastest interval – 1 min
Priority - PRIORITY_BALANCED_POWER_ACCURACY
In a normal running scenario, the location updates are received properly and the location accuracy is pretty good (around 20-25 m).
When the Maps application is opened, it received GPS location, and therefore our application also starts receiving location updates, which are basically from the GPS transceiver (I know this as the subsequent location updates have a speed and altitude value as well).
Problem occurs when the Maps application is now closed. Our application receives location updates, but the location coordinates do not change and every subsequent update received has a higher accuracy value. The updates may go up to even 1000m of accuracy and no accurate location are received. Opening the maps application again will make our application receive accurate and updated locations once again.
This problem can also be device specific as I saw this problem on a Motorola Moto G and not on my other Samsung or Karbonn phones.
In my experience, setting the priority to PRIORITY_BALANCED_POWER_ACCURACY never gets positions from the GPS, unless other app is using it. That's the same behaviour that you are finding in your app (in your case you only get GPS positioning when Maps is open), and that seems to be the general behaviour (see Does PRIORITY_BALANCED_POWER_ACCURACY exclude the GPS provider?).
I haven't found any official documentation to background this so I think that it may be device-dependant. In my tests, whenever I need GPS positioning I need to change the priority to PRIORITY_HIGH_ACCURACY.
Anyway, there is a known issue (57707) that is showing in some devices that causes the location provider to stop receiving position updates when set in PRIORITY_BALANCED_POWER_ACCURACY until the device is restarted.
I've been working on an app that needs to be location-aware, and I've noticed that there are two (or more) methods of receiving location: with Google Play services (as seen here developer.android.com/training/location/retrieve-current.html#GetLocation) and with Location Manager, Providers etc. (as seen here http://www.vogella.com/tutorials/AndroidLocationAPI/article.html#locationapi_criteria).
What is the difference between these methods (if there is any)? Which one is more accurate?
edit: ok, I see that I sent the wrong link on the first thing. Won't this code (http://developer.android.com/training/location/receive-location-updates.html) give me location updates? Generally, what's the most accurate way to get my location?
The one with the GPS is accurate and that which is based on Network is not. Google Play Service use FUSE api to get the GPS location first, if the location is found (that's great), otherwise it will try to get location fix from Network Tower. In Short the one with GPS is accurate
The first method provides the details of LastKnownLocation. ie. the last location received from GPS or network provider when you or other apps accessed the location services. After that there are chances you moved a lot and it need not be your current location. So if You are planning to create an application that requires accurate location tracing, You should fetch the location as in the "Vogella" method. If the current location is unavailable, you can try using the last known location (As a plan B :-)).
Am working with the location update application which will update the user location periodically to the server.
I have used the fused location provider to get the location update. I have referred the following link Android Location Update.
Regarding the location update I have following clarifications,
1) I requested Location using Pending Intent. I had given the Time
interval as 5 min in the location request. I am getting the location
information successfully at every 5 min. But my question is "How effectively
Android uses the GPS to get the location - GPS need not to be on for
the whole 5 min". Around 4 min 50 seconds it starts using the GPS to
get the user location. I just want to know how the fused location provider effectively using the GPS.
2) And also I want to know the time taken to fetch the user location
by using available providers. How much time approximately taken by the
android api to get the user location by using fusion location
provider.
3) The Fused Location Provider uses GPS only at the nearest time of given interval to get
the user location. The remaining time the GPS is not usable. Is there
is better solution to switch off the GPs or effective way to use the
GPS to save the mobile battery power.
4) Am giving the time interval as 1 min. Some time am not getting the
location update every one minute. For Example, First minute successfully
I am getting the user location. Then not getting the user location at 2nd Minute. Then 3rd minute
getting fine and so. I have GPS ON and with mobile network available and WiFi
connected.
5) And what is the maximum time interval we can give. Am not finding
any maximum time limit in the document. We can give the minimum time interval as zero. But it is not recommended.
6) And also I want to know the minimum OS support while using this feature. I have referred below link Does Google Activity Recognition work on older versions of Android? Which says Everything in Google Play Services should work back to API level 8(Android 2.3).
Please help me on this. I hope this could help other developer also who are working on Android Location Updates.
Thanks in advance.
I have used the fused location provider API's in our project and it's was really helpful to improve battery usage.Earlier We were using Android's Location framework APi's .
Please read below article which I have prepared ,you can refer the information to implement it in your project.
In simple words, it’s the best way to get location data in Android platform as of now.
GooglePlay Services provide many location APi’s to get the location data(e.g. User’s current location or you can say device’s last known location).
The Fused Location Provider is one of the location APIs in Google Play services.
Prerequisite is that:
1- Google Play services sdk is used as library project(and also Google PlayService is properly installed in your device)
Download and install the Google Play services component from the SDK Manager and add the library to your project.
Import GooglePlayServices lib from android google-play-services_lib in your development project as Library project.
2- You should have an actual device as this APi won’t work in Emulator.
The Fused Location Provider intelligently manages the underlying location technology (GPS/Wi-Fi/Network provider’s connection) and gives us the best location according to our needs.
Why to use
=============
We could choose one of the location providers (network or GPS) and request location updates or set up proximity alert. But there were two main issues with this approach:
1. In case we need to define precise location, we had to switch between network and GPS location providers (as GPS doesn’t work indoors).
2. Proximity alerts were used to notify a user about proximity to a location, and this took its toll on the battery life.
Benefits
==========
1. Simple APIs: Lets us specify high-level needs like “high accuracy” or “low power”, instead of having to worry about location providers.
2. Battery efficient: Minimizes out app’s use of power. Based on all incoming location requests and available sensors, fused location provider chooses the most efficient way to meet those needs.
Steps to use this Api:
=====================
1- Declare the location related permission in the manifest file.
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
Or
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2. Implement related interfaces and callbacks.
Before we request location updates, we must first implement the interfaces that Location Services uses to communicate connection status to our app:
2.1 com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks: Specifies methods that Location Services calls when a location client is connected or disconnected.
2.2
com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener
3 Connect to Google Play Services
Connecting LocationClient to Google api.To do this , create a LocationClient object (it’s actually instance of GoogleApiClient object) as below:
mLocationClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
and then call connect() :
mLocationClient.connect();
4- Create an instance of FusedLocationProviderApi by using LocationServices class as below:
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
5- Retrieve the current location
Inside onConnected(Bundle bundle){
Location currentLocation = fusedLocationProviderApi .getLastLocation(mLocationClient);
if (mCurrentLocation != null) {
Log.d(TAG, "last location =" + mCurrentLocation.getLatitude()
+ " - " + mCurrentLocation.getLongitude());
}
}
6- Receive periodic location updates
Create and setup LocationRequest object:
mLocationRequest = new LocationRequest();
private void setLocationParameter() {
// Set the update interval
mLocationRequest.setInterval(Constants.SECONDS_TO_UP);
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest.setFastestInterval(Constants.SECONDS_TO_UP);
// Set the distance to 10 meters.
mLocationRequest.setSmallestDisplacement(Constants.METERS_TO_UP);
}
6- Request for periodic Location updates: To get periodic location updates from Location Services, we send a request using a location client.
LocationListener listener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Utils.locationUpdates = Utils.locationUpdates + 1;
if (Utils.locationUpdates == 1) {
mLocationRequest
.setPriority(LocationRequest.PRIORITY_LOW_POWER);
LocationServices.FusedLocationApi.requestLocationUpdates(
mLocationClient, mLocationRequest, listener);
}
}
}
};
I am developing an android application wherein I need the user location updates pretty frequently. Say 2 times a minute.
Earlier I had been using Google Play Service's "Fused location service" but the location updates were not received as requested.
The location updates got stuck for sometime, the interval between updates jumped to 10min or so.Sometimes even if I put my priority to "PRIORITY_HIGH_ACCURACY" the same happened.
I then went back to the old "Location Manager" and when I used the "NETWORK_PROVIDER", I noticed that the location updates got stuck due to this provider. Also the GPS does not get activated immediately, it takes some time. I am trying to build my custom fused location provider. How can I efficiently switch between providers, without getting lags on location updates.
I want to know what are the best practices for getting location updates regularly, all the time, be it either NW, GPS or both. Like it should work for an application where location updates getting stuck cannot be afforded.
Battery drain is not an issue for me right now.I am aware of all the supporting docs that Google provides regarding location access.
Any help would be much appreciated.
Thankyou !
FusedLocationProvider really is the best option for obtaining locations at the moment, because it uses a lot more than just GPS or Network data to obtain location fixes. I have experienced issues regarding intervals being missed as well, but ultimately this is something down to luck depending on availability of GPS, Network, etc. etc.
My favourite usage of FusedLocationProvider so far is in conjunction with the AlarmManager class: Basically, the idea is to start location tracking at intervals specified by the Alarm Manager (this can be set to every 30 seconds such as in your case). Once the interval is hit, the location provider is to obtain a location fix as soon as possible (so in the setInterval method or whatever it's called, the parameter is a 0). This way, you can avoid having to wait another 30 seconds for a new location, instead having the location tracker attempt to provide a location as soon as possible after the interval specified by the Alarm Manager is hit.
By the way, when making custom location tracking wrappers, be careful of using the .getLastKnownLocation() method as it only uses cached locations - you could end up sending the same location to the user every 30 seconds.
Good luck!
I am writing an app which needs to get any fresh location. Accuracy does not metter. My app will mainly be used in buildings, so I am using Network location provider as a main location source.
I have troubles on some devices as follows:
My code can't access any location. But when I start Google Maps on the same device - it shows correct location. I switch back to my program and it does not see any location again.
My code does roughly the following:
Check if Network location provider is available with code below:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
If network provider is enabled - requirest single location update:
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Code also starts Timer for 5 seconds. If code does not get requested location update during 5 seconds, then timer is iterating throuhg all available providers trying to get last known location and pick up the best one:
Location location = locationManager.getLastKnownLocation(provider);
The result of all these moves is that network provider is On, but code does not get any updates. Timer starts, loops through all providers and all of them does not have any last known location. getLastKnownLocation method returns null for all of them. The most strage thing is that Google Maps got correct location just a few seconds before this. I've rebooted device with no effect.
I examined best practices of getting location here. They seem to use the same approch as I currently do. Are there any tricks I am missing?
This problem occur in some devices the solution is try to get location from GPS if fails then get location from WIFI device