app with GPS-enable but the device is indoor

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

app with GPS-enable but the device is indoor

Postby twsc » Tue Nov 17, 2009 1:14 am

Hi All,

I want to detect if the GPS is enable first.

if it is outdoor, then I use the following criteria.

And then if it is indoor or the GPS signal is not able to get, then the location provider will change the criteria to Criteria. ACCURACY_COARSE.

Does anybody know how to make it? Thank you so much.

//////////////////////////
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);

//////////

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
twsc
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Jan 28, 2009 2:23 pm

Top

Re: app with GPS-enable but the device is indoor

Postby jagtap.jj1 » Tue Nov 17, 2009 5:04 am

Pool The gps points using both listener

LocationListener [] locationListeners = new LocationListener[]
{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};

And collect the gps points from both listener, if it is indoor u will get gps points from LocationManager.NETWORK_PROVIDER. if its open sky u will get fine accuracy from LocationManager.GPS_PROVIDER.

twsc wrote:Hi All,

I want to detect if the GPS is enable first.

if it is outdoor, then I use the following criteria.

And then if it is indoor or the GPS signal is not able to get, then the location provider will change the criteria to Criteria. ACCURACY_COARSE.

Does anybody know how to make it? Thank you so much.

//////////////////////////
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);

//////////

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
java-android developer
User avatar
jagtap.jj1
Senior Developer
Senior Developer
 
Posts: 124
Joined: Fri Aug 22, 2008 11:52 am
Location: India(Mumbai)

Postby Ressor » Sat Dec 12, 2009 6:58 am

Any chance you can show the whole code to do this. I've tried to edit my working GPS listener app and can't get past the errors.
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

Postby jagtap.jj1 » Wed Dec 30, 2009 10:58 am

Here is source code:



import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;


public class MyLocationManager {

private static final String TAG = "MyLocationManager ";

private Context mContext = null;
private LocationManager mLocationManager = null;

public MyLocationManager(Context context)
{
this.mContext = context;

if(this.mContext != null)
{
this.mLocationManager = (LocationManager) this.mContext.getSystemService(Context.LOCATION_SERVICE);
}
}

LocationListener [] mLocationListeners = new LocationListener[]
{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};

private class LocationListener implements android.location.LocationListener
{
Location mLastLocation;
boolean mValid = false;
String mProvider;

public LocationListener(String provider)
{
mProvider = provider;
mLastLocation = new Location(mProvider);
}

public void onLocationChanged(Location newLocation)
{
if (newLocation.getLatitude() == 0.0 && newLocation.getLongitude() == 0.0)
{
// Hack to filter out 0.0,0.0 locations
return;
}
if (newLocation != null)
{
///if(newLocation.getTime() == 0) newLocation.setTime(System.currentTimeMillis());
newLocation.setTime(System.currentTimeMillis());

if(Config.DEBUG)
{
Log.i(TAG, "onLocationChanged in loc mgnr");
}
}
mLastLocation.set(newLocation);
mValid = true;
}

public void onProviderEnabled(String provider) {
}

public void onProviderDisabled(String provider)
{
mValid = false;
}

public void onStatusChanged(String provider, int status, Bundle extras)
{
if (status == LocationProvider.OUT_OF_SERVICE)
{
mValid = false;
}
}

public Location current()
{
return mValid ? mLastLocation : null;
}
};

public void startLocationReceiving()
{
if (this.mLocationManager != null)
{
try
{
this.mLocationManager.requestLocationUpdates
(
LocationManager.NETWORK_PROVIDER,
1000,
0F,
this.mLocationListeners[1]);
}
catch (java.lang.SecurityException ex)
{
if(Config.DEBUG)
{
Log.e(TAG, "SecurityException " + ex.getMessage());
}
}
catch (IllegalArgumentException ex)
{
//Log.e(TAG, "provider does not exist " + ex.getMessage());
}
try
{
this.mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000,
0F,
this.mLocationListeners[0]);
}
catch (java.lang.SecurityException ex)
{
if(Config.DEBUG)
{
Log.e(TAG, "SecurityException " + ex.getMessage());
}
}
catch (IllegalArgumentException ex)
{
//Log.e(TAG, "provider does not exist " + ex.getMessage());
}
}
}

public void stopLocationReceiving()
{
if (this.mLocationManager != null)
{
for (int i = 0; i < this.mLocationListeners.length; i++)
{
try
{
this.mLocationManager.removeUpdates(mLocationListeners[i]);
}
catch (Exception ex)
{
// ok
}
}
}
}

public Location getCurrentLocation()
{
Location l = null;

// go in best to worst order
for (int i = 0; i < this.mLocationListeners.length; i++)
{
l = this.mLocationListeners[i].current();
if (l != null)
break;
}

return l;
}
}
java-android developer
User avatar
jagtap.jj1
Senior Developer
Senior Developer
 
Posts: 124
Joined: Fri Aug 22, 2008 11:52 am
Location: India(Mumbai)

Postby Ressor » Sun Jan 03, 2010 4:12 am

Thank you!
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

View Logs

Postby araja » Wed Feb 17, 2010 7:49 am

Where to view the logs we have logged using Log.i()?
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Top

Postby Ressor » Wed Feb 17, 2010 3:15 pm

If you are using eclipse you can view them in the "Logcat" view/tab while you execute the application.
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

Postby araja » Wed Feb 17, 2010 3:27 pm

I think this works only for debug mode. What if I want to see the log when application is not running in the debug mode? I want to carry the device to different locations to test it.


Thanks
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby Ressor » Wed Feb 17, 2010 3:30 pm

Sorry, I assumed you were develping and using the emulator. I think you might want to change the LOG line to Toasts if you are working with a live device and want to see the output real-time.
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

Postby araja » Thu Feb 18, 2010 8:08 am

Thanks, it worked.
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Unable to get Location using Cell Tower

Postby araja » Mon Mar 08, 2010 8:05 am

Hi Ressor,

I have used the above code and able to find location when the device is indoor. However, I am facing a problem, if the SIM is inserted and Edge is enabled then the Network Provider returns accurate location. But, if SIM is inserted and Edge is disabled the Network Provider returns the too far location.

I think, if SIM is inserted it should triangulate correctly and provide accurate location no matter if Edge is enabled or not.


Any ideas?


Thanks

Adnan
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby araja » Thu Mar 11, 2010 2:15 pm

Any one???
araja
Developer
Developer
 
Posts: 27
Joined: Wed Nov 04, 2009 9:03 am

Postby Ressor » Mon Mar 29, 2010 3:34 pm

I'm just about to try to use this code myself so I'll report back if I find any issues or can see that same problem you see. I'm not sure I understand the issue though and don't know what you mean by SIM and EDGE. I think if you reword the issue with GPS and NETWORK, I might understand better or the senior developers can help us both.
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

Postby Ressor » Mon Mar 29, 2010 5:38 pm

Ok.. I'm having some pretty basic trouble due to my own stupidity. Can someone post the code to instantiate this from another activity?
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

Postby Ressor » Mon Mar 29, 2010 6:26 pm

Opps! I freaked out too soon and figure it out on my own.
Ressor
Developer
Developer
 
Posts: 28
Joined: Wed Oct 14, 2009 11:43 pm
Location: Boston MA USA

Top
Next

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 4 guests