I'm trying to get location manager to work in an Activity in my Android app but it keeps giving me the error "cannot resolve method requestLocationUpdates"
I'm putting this code in my OnCreate (found from other examples):
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
I want to use my own "OnLocationChanged" method to update a map with the users location, but I can't figure out why I'm getting this error.
Any ideas why I'm getting the error?
Is there a better more modern way to implement location updates?
Thanks!
I would recommend you look into the new FusedLocationProviderApi that is part of Google Play Services. From the docs:
The Google Location Services API, part of Google Play Services,
provides a more powerful, high-level framework that automatically
handles location providers, user movement, and location accuracy. It
also handles location update scheduling based on power consumption
parameters you provide. In most cases, you'll get better battery
performance, as well as more appropriate accuracy, by using the
Location Services API.
Make sure you're important the correct LocationClient, should be com.google.android.gms.location.LocationClient and you might be importing android.location.LocationClient
Related
I'm working on location based app. I am using LocationUpdatesForegroundService sample code.
Which is a Service to get location details
At some case location are not getting.
like.
GPS and Internet are on I'm getting location details.
but,
GPS and Internet are in offline I'm not getting location details.
case :
Start app I am start to service getting location but gps off case to first prompt to please gps ON after I gps ON.
BUT not getting LOCATION mFusedLocationClient.getLastLocation method task.getResult() are null.
please resolve my issue.
Thanks
NOTE:
This code working in MOTO E os version 4.4.4.
but not working in LAVA a97 os version 6.0
I'm using this sample :-
https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesForegroundService
You can get only details by getting LocationUpdates in textview maps won't give you updates in offline.If you are using textview for getting updates You may forgot this.
add CourseLocation Permisssion in Manifest.xml
In onCreate()
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
and finally don't forget to add permission in manifest and handle the permissions in Java code too because of dynamic permissions.
That's it.
I hope it will work fine even in offline also.
j know that since 4.0 it s impossible to trigger programmatically gps
but besides that there are three possibilities to localise
1) gps and wifi and mobile network (all together)
2) only wifi and mobile network
3) only gps
is there some possible code to get through the second one
so let's be clear j don't want to trigger wifi. that i know
j want to trigger localisation by whatever wifi (and)or mobile network without using gps
j tried to implement some object like skyhook' WPSPeriodicLocationCallback or WPSLocationCallback but
it doesn 't work without triggering the official android security menu
so what j want is getting through the positionning system only with wifi or internet connection and that by code
avast antitheft does that giving back some information with more or less accuracy .
i would like to reproduce the same
thanks in advance
You can get locating the position using the LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.
http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.
After you get done with most of the code in OnCreate(), add this:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
You could also have your activity implement the LocationListener class and thus implement onLocationChanged() in your activity or you can use this tutorial to get lat and lang.
Copypasted from this answer.
I am developing a location based app which have the functionality to update user current location in every 1 minutes.
I am using bellow to code for requesting location updates:
private LocationRequest mLocationRequest;
private static LocationClient mLocationClient;
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(60000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setFastestInterval(60000);
mLocationClient = new LocationClient(this, this, this);
if (servicesConnected()) {
mLocationClient.connect();
}
servicesConnected() is user defined method which returns true if Google play services is available otherwise returns false
and my overriden method like this:
#Override
public void onConnected(Bundle connectionHint) {
try {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
} catch (IllegalStateException e) {
// TODO: handle exception
}
}
#Override
public void onLocationChanged(Location location) {
// logic to store location data
}
But I found location updates like bellow figure while my GPS is ON :
Please suggest what should I do to overcome unwanted location updates.
Here's some info from OwnTracks https://github.com/owntracks/android/issues/66
From my research the only thing you can do is filter out "bad" locations like this:
#Override
public void onLocationChanged(Location location) {
if(location.getAccuracy() < 210.0) {
// use provided location
}
}
It doesn't appear that there is any way to prevent these updates from being requested using the Fused provider (other than stopping updates or increasing the interval when you have a location you are satisfied with). There's only so much "filtering" that can be done by the GPS itself and those options are the constants inside LocationRequest that you already know about. I believe your issue is hardware related or has to do with the location you are getting updates from (I'm basing this assumption on the OwnTracks data). Google could theoretically offer more advanced Criteria like it used to do with LocationManager, but I believe that would basically do the same thing as my example, except under the hood (i.e. the GPS would still do the work of getting the location and then discard it AFTER it knows the accuracy isn't high enough).
If you want to make it do less work, your best options are increasing the interval or simply stopping updates when you no longer need new locations. For example, if you have a decent location, maybe you raise the interval and keep that location longer. But that depends on what your app is trying to do with the data.
I'm having little problem with getting my current location using NETWORK_PROVIDDER.
My code looks like this:
LocationManager lMgr = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
boolean isNetworkProviderEnabled = lMgr
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location location = lMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
// deal somehow with last (very likely outdated location)
}
if (isNetworkProviderEnabled) {
lMgr.requestLocationUpdates(provider, 0, 0, locationListener);
}
Of course I have MyLocationListener that deals with location changes and ALL REQUIRED PERMISSIONS ARE ADDED TO MANIFEST
And the problem is that on some phones this code works like charm, but one others, the "requestLocationUpdates" does totally nothing. Of course on those problematic phones, when I open Google Maps application, my current location appears immediately. So my question is (I believe that people from Google should answer this): how this is done that Google Maps retrieves current location immediately, and other apps don't? Is my code wrong? Of course I have seen code like this in many stackoverflow questions. If anyone wish to know, this kind of problem appears on some Samsung Galaxy Nexus S phones
In my application GPS is not used to save battery power, but location services are enabled.
I have recently faced the same issue, LocationManager is buggy and not implemented completely on Samsung phones. They also consume a lot of power. Use LocationClient to solve both these issues. LocationClient uses all means available to get you the last location, so its the super set of all your providers and sensors.
I faced issues with the Samsung Y model phones, I wonder which phones you are talking about. To fix the problem with Samsung phones, I kickstarted the GPS on the phone with
HomeScreen.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(final Location location) {
}
});
And then called the LocationManager.getLastKnownLocation OR the new locationClient.getLastLocation api.
First, just add the above lines over your getLastKnownLocation. Things should magically start workings
Migrate to LocationClient when you have more time and test again. Use the magic above to get it working if it fails.
Sometimes device does not receive any location updates and GPS_PROVIDER and NETWORK_PROVIDER returns null.
Try restarting your device, this trick helped me. My code was perfectly fine but providers were returning null.
How to read location only once with locationManager (GPS and NETWORK PROVIDER) and not any more looking for updates of location, to save battery?
Although requestSingleUpdate() is the technically correct answer, you should use
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, mLocationListener);
Wait for a while after getting your first location. The location tends to wobble for a few seconds. To determine if the fix is stable use, location.getAccuracy(). Once the accuracy stabilizes, call locationManager.removeUpdates(mLocationListener);
public LocationManager locationManager;
try {
locationManager.requestSingleUpdate( LocationManager.GPS_PROVIDER, new MyLocationListenerGPS(), null );
} catch ( SecurityException e ) { e.printStackTrace(); }
public class MyLocationListenerGPS implements LocationListener {
#Override
public void onLocationChanged(Location location) {
// your code and required methods go here...
}
Using 'null' in 'requestSingleUpdate' keeps the update check on the same thread (for use in a service). The method allows for you to put it in a Looper if you're running it from an activity. I would think that using an async task would be the better approach.
Just remove updated from the location manager.
locationManager.removeUpdates(this);
Please read Obtaining User Location developer guide :
http://developer.android.com/guide/topics/location/obtaining-user-location.html
This link for best Performance
Don't forget to specify android.permission.ACCESS_FINE_LOCATION in the Android manifest
To get the location only once you can refer this link and search for the requestSingleUpdate
Its easy just once read the loacation update like
locationManager.requestLocationUpdates(provider, 400, 1, this);
and after reading the location once called
locationManager.removeUpdates(this);
so system will not ask for location update after this and you cab save your battery.
Using requestSingleUpdate with your LocationManager.
For more info, official guide is interesting:
INFO