0

I am developing small location based android application in which I need users current location. I am also updating users current location as soon as some change in location is occurred.My code looks like:

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

}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
}; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LocationManager locationManager;
    String svcName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(svcName);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setSpeedRequired(false);
    criteria.setCostAllowed(true);
    String provider = locationManager.getBestProvider(criteria, true);

    //Location l = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
    Location l = locationManager.getLastKnownLocation(provider);

    updateWithNewLocation(l);

    locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);

}


private void updateWithNewLocation(Location location) 
{
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    String latLongString = "No location found";
    if (location != null) {
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    latLongString = "Lat:" + lat + "\nLong:" + lng;
    }
myLocationText.setText("Your Current Position is:\n" +
latLongString);

}

My problem is that when I use provider as network then it's working fine. But when it select gps as provider then it's giving null value. I know for the first time it gives me null value. I also used onLocationChanged method but it's still not giving me proper output. When I open my application it shows me output null value and it also start gps for serching my location. I wait for some time to get updated location but it not giving me valid output. Is there any thing wrong with my code. I am using android device.

Need Help... Thank you...

nilkash
  • 7,408
  • 32
  • 99
  • 176
  • 1
    Possible duplicate http://stackoverflow.com/questions/6556389/how-to-get-user-location-geo-points-in-android – DynamicMind Jan 02 '13 at 09:30
  • Possible duplicate http://stackoverflow.com/questions/5674414/location-returned-is-null-when-provider-is-gps –  Jan 02 '13 at 09:41
  • possible duplicate of [How do I get the current GPS location programmatically in Android?](http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) – Siddharth Lele Jan 02 '13 at 13:55

4 Answers4

0

Change this line

Location l = locationManager.getLastKnownLocation(provider); 
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);

to

Location l = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

then i think it will work.

Or you can follow this tutorial to get the location using gps

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Chinmoy Debnath
  • 2,814
  • 16
  • 20
  • Hye chinmoy thanks for quick reply. I tried your solution but it gives me same problem. if I use network as provider then it gives me proper output.Any other solution... – nilkash Jan 02 '13 at 09:38
  • Can you follow the tutorial links that i have added please. – Chinmoy Debnath Jan 02 '13 at 09:47
0

Have you entered these permission in the manifest?

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>
Alex
  • 149
  • 1
  • 6
  • Hi alex thank you for reply I added above permission in my manifest file. But gives same issue. Any other solution. – nilkash Jan 02 '13 at 09:41
0

When You use the Provider as Network, then you get the location immediately as it gets the Location values of nearest Tower of the network provider. But when you use GPS, it takes some time to fetch the current location values. In your case (after seeing code) it is very much clear that you are getting last known location using Network Provider so it gets the location and when use gps provider it takes some time and more importantly if you are in close area, takes more time to get location values through gps provider.

TNR
  • 5,839
  • 3
  • 33
  • 62
0

here,system is trying to select best provider according to the criteria you have set

e.g. criteria.setPowerRequirement(Criteria.POWER_LOW);

but GPS provider doesn't fit with the criteria which you have specified. that is why it may not working properly.either change the criteria or if you want to get location updates from GPS provider only then use following code,

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000, 10, locationListener);
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57