How to get initial location with LocationManager? - android

I've been getting current location with GoogleApiClient up until now but I've just noticed that it's much simpler to do it with LocationManager using LocationListener since it can even detect when GPS service was turned on or off by the user.
But I have a problem when getting user's first location after the LocationManager was initialized.
LocationManager has 4 listeners but none of them give you your first location. It does have a onLocationChanged listener but it only activates when you move.
This is how I'm using it:
// Init LocationManager (needed to track if GPS is turned on or not
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
end of oncreate......
/*
LocationListener (Listening if GPS service is turned on/off)
*/
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderDisabled(String provider) {
}

Related

Android locationManager requestLocationUpdates not called

I'm making an android application which keeps track of your location to eventually draw your route on a google map. Everything seems to be working except for one thing which, strangely enough, did work before. I think I did not change anything that matters to the function call and most examples I found did it the same way I did.
In my onCreate method I do the following:
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 2000, 0, locationListener);
later i call upon the LocationListener:
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
but somehow this does not get called. Tried logging everywhere, everything returned a value except for the ones in the onLocationChanged. Doesn't work with GPS_PROVIDER either
All the relevant code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracking);
setUpMapIfNeeded();
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Tell the program to get location updates every 2 seconds(2000ms)
// I sadly don't get called..
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 2000, 0, locationListener);
}
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
System.out.Println("I'm just here for debugging purposes");
System.out.Println("Sadly I don't appear in Logcat");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
Did you add permission in manifest file?

How to observe Gps Provider continually?

I want to know when the GPS on the device is tuned on, and the location is changed.
Here is what I've done:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
Toast.makeText( getApplicationContext(),"onlocation changed",Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"providerdisabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"provideenabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
It is working fine when my app runs in the foreground, but when I close it, I won't get any more updates. I'd like to know how to get this information continuosly until my application is uninstalled.
You need to put this in a service and start the service sticky.
Also you should think about using the FusedLocationManager by using GooglePlayServices.
For more info about GooglePlayServices and locations read here

Get accurate current location from Network Provider

I use the following code to get Current Location from a Network provider in my application:
LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean network_enabled = mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(network_enabled){
Location location = mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
But it gives a location approximately 300-700 meters away from my actual location.
This is expected from a Network provider. But the problem is:
with only this Network provider enabled, and no GPS, I openned Foursquare
application where it shows my current location exactly where I am. Now
when I come back to my application, it shows the accurate current
location or say the same location which Foursquare showed.
Same thing happens with Google apps like Navigator, Maps etc..,
How can this be done? How are other apps able to get the exact location, based on just the Network provider?
Complete Code:
public class MyLocationActivity extends Activity implements LocationListener {
private LocationManager mgr;
private String best;
Location location;
public static double myLocationLatitude;
public static double myLocationLongitude;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
location = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
dumpLocation(location);
}
public void onLocationChanged(Location location) {
dumpLocation(location);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
protected void onPause() {
super.onPause();
mgr.removeUpdates(this);
}
#Override
protected void onResume() {
super.onResume();
mgr.requestLocationUpdates(best, 15000, 10, this);
}
private void dumpLocation(Location l) {
if (l != null){
myLocationLatitude = l.getLatitude();
myLocationLongitude = l.getLongitude();
}
}
}
Thank You
You are getting just the last known location. You should request location updates from the LocationManager. Use LocationManager.getLocationUpdates.
On your LocationListener on the onLocationChanged(Location location) method, you can check on the Location object, how accurate this location is, like this :
float accuracy=location.getAccuracy();
then, if this Location is accurate enough for you, you can stop receiving location updates from the LocationManager using removeUpdates() , and use the received Location. If the Location is not accurate enough, you can wait for a more precise Location, and stop the updates latter on.

Unable to get the location updates in monodroid

I am trying to get the location in my Android app as follows
LocationManager lmanager = (LocationManager)context.GetSystemService(Context.LocationService);
if (lmanager.IsProviderEnabled(LocationManager.NetworkProvider))
{
Location currentLoc = lmanager.GetLastKnownLocation(LocationManager.NetworkProvider);
}
I tried with LocationManager.gpsProvider also instead of the Network provider.
In all the cases I am getting a null reference exception for currentLoc.
The GetLastKnownLocation() method returns the last location it knew about, which could be null if it has never received any updates before. I think what you want to do here is listen for location updates, and then respond to them once they come in. You can do this by implementing the ILocationListener interface:
class MyLocationListener : Java.Lang.Object, ILocationListener
{
public void OnLocationChanged(Location location)
{
}
public void OnProviderDisabled(string provider)
{
}
public void OnProviderEnabled(string provider)
{
}
public void OnStatusChanged(string provider, Availability status, Bundle extras)
{
}
}
In your activity you can declare that you'd like to use this class to start listening for updates like this:
var locationManager = (LocationManager)GetSystemService(LocationService);
var locationListener = new MyLocationListener();
locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, 5000, 2, locationListener);

How to determine whether GPS position has been acquired on android?

Let's say I have a button which calls another activity only if the system knows the EXACT user's location. I made a dialog to ask the user to turn on GPS if it's disabled, but when the user enables GPS and pushes the button again, I will probably not have the exact location yet. Can anyone help me with this ?
You need to create a LocationListener, and wait for the callback. It is pretty well described in the api docs:
// 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) {
// When you get here you have a location...
useTheNewLocationToEnableTheButton(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.GPS_PROVIDER, 0, 0, locationListener);
You can listen for GpsStatus events, to make sure that user actually enabled GPS (GPS_EVENT_STARTED), and to be notified when GPS gets position fix (GPS_EVENT_FIRST_FIX).
If you don't know coordinates, the best way is to display a ProgressDialog while retrieving a location and, when you got it, dismiss ProgressDialog and launch your activity.
final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//here you can get coordinates via location.getLongitude() and
//location.getLatitude() methods, dismiss the progress dialog and
//start your activity
locationManager.removeUpdates(this);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

Categories

Resources