3

I'm trying to get the latitude and longitude information from an android phone through GPS, when i'm outdoor or under the sky directly i'm able to get the values instantly but when i'm indoor or inside a room its taking more than a minute to get the values. Can anyone help me in getting this values fastly when I'm using my app inside a room.

I'm using the following code in getting the values:

LocationManager locManager;
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
                                  locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

and

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {

    EditText myLocationText = (EditText)findViewById(R.id.editText1);
    EditText myLocationText1 = (EditText)findViewById(R.id.editText2);
    String latString = "";
    String LongString = "";

    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        latString = "" + lat;
        LongString ="" + lng;



    } else {
        latString = "No location found";
        LongString = "No location found";
           }
     myLocationText.setText(""+ latString);
     myLocationText1.setText(""+ LongString);
}

Is there any other way in getting the GPS values other than using LocationManager??

ksushanth
  • 31
  • 2
  • I think you will want to use a gps cache. Checkout this link : http://forums.groundspeak.com/GC/index.php?showtopic=215136 . They are comparing various tools for this. Maybe one of them would fit your purpose? – Kevin Coulombe Nov 28 '11 at 14:18
  • 1
    If your app can't receive a signal from the satellites, then of course the GPS won't work. If you get any signal at all indoors, then that's a bonus. You wouldn't expect your TV to work without an aerial. – NickT Nov 28 '11 at 14:19
  • @KevinCoulombe [Geocaching](http://en.wikipedia.org/wiki/Geocaching) is a sport, are you sure that link is relevant? – skynet Nov 28 '11 at 14:20
  • Awww, disregard my comment! It's a tool for caches as in treasure hunt caches... – Kevin Coulombe Nov 28 '11 at 14:21

5 Answers5

1

You can get the last known location, and it's quite fast:

/**
 * Gets the last known location.
 *
 * @param locationManager the location manager
 * @return the last known location
 */
public static Location getLastKnownLocation(LocationManager locationManager)
{
    Location bestResult = null;
    float bestAccuracy = 10000;
    long bestTime = 0;

    List<String> matchingProviders = locationManager.getAllProviders();

    for (String provider: matchingProviders) {
        Log.d("LOCATION", "Provider: " + provider);
        Location location = locationManager.getLastKnownLocation(provider);
        Log.d("LOCATION", "Location found? "+ (location==null?"NO":"YES"));
        if (location != null) {
            float accuracy = location.getAccuracy();
            long time = location.getTime();
            Log.d("LOCATION", "Acc: "+ String.valueOf(accuracy) + " -- Time: " + String.valueOf(time));
            if ((time > minTime && accuracy < bestAccuracy)) {
                bestResult = location;
                bestAccuracy = accuracy;
                bestTime = time;
            }
            else if (time < minTime && 
                    bestAccuracy == Float.MAX_VALUE && time > bestTime){
                bestResult = location;
                bestTime = time;
            }
        }
    }
    Log.d("LOCATION", "BEST FOUND? "+ (bestResult==null?"NO":"YES"));

    return bestResult;
}
SERPRO
  • 10,015
  • 8
  • 46
  • 63
0

I used Network provider to fetch the Co-Ordinates and it is always much faster inside the house in living room. But when I try the same in the Basement area, it doesn't fetch the coordinates at all though I waited almost couple of minutes.

Note: I could able to make a call from basement area and I am also able to browse as well..

Ramesh Sangili
  • 1,633
  • 3
  • 17
  • 31
0

You can get the last known location of the device using the following method in LocationManager, this is the location that has been last found system-wide.

This method is the key:

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

See for more details Obtaining User Location

Zebaz
  • 1,545
  • 15
  • 11
0

If you are indoors, you would be better of using LocationManager.NETWORK_PROVIDER as you are unlikely to get a signal inside. You could also use getLastKnownLocation(), but this may be non existent or out of date.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
0

If you want get position in a room, gps provider is slowly. You can try changing:

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
                                  locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

By:

locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000L,500.0f,
                                      locationListener);

And you will receive location in 'public void onLocationChanged(Location location)' method.

frayab
  • 2,512
  • 20
  • 25