views:

156

answers:

2

I am having a problem in getting GPS coordinates in 2.1.

The code i am using right now is working well in 1.6 but when i test this same apk in 1.6 device is showing null values

plz help me to find a way to work with 2.1 devices also

Here is my code...

public class GpsLocator {

   private static String PROVIDER="gps";
   private LocationManager myLocationManager=null;

   public GpsLocator(Context context) {
      myLocationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
   }

   public void myOnresume() {
      myLocationManager.requestLocationUpdates(PROVIDER, 0,   0, onLocationChange);
   }

   public void myonPause() {
       myLocationManager.removeUpdates(onLocationChange);
   }

   public double getLatitude() {
      Location loc=myLocationManager.getLastKnownLocation(PROVIDER);
      if (loc==null) {
         return(0);
      }
      return(loc.getLatitude());
   }

   public double getLongitude() {
      Location loc=myLocationManager.getLastKnownLocation(PROVIDER);
      if (loc==null) {
         return(0);
      }

      return(loc.getLongitude());
   }

   LocationListener onLocationChange=new LocationListener() {

      public void onLocationChanged(Location location) {
      }

      public void onProviderDisabled(String provider) {
         // required for interface, not used
      }

      public void onProviderEnabled(String provider) {
         // required for interface, not used
      }

      public void onStatusChanged(String provider, int status,Bundle extras) {
         // required for interface, not used
      }
   };
}

in the manifest file i add permission for accessing file they are

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+1  A: 

LocationManager.getLastKnownLocation does not guarantee that it will return a valid location. If a GPS fix hasn't been established yet, it will return null. The only reliable way to get an actual location is to use the LocationListener -interface. I see that you have defined a LocationListener, but aren't using it.

You need to modify your code so that it waits for the first call to onLocationChanged before you try to do anything with the location.

TuomasR
thank u tuomasR for u r suggestions, i even tried in that also.
Ramesh Bugatha
Any way now i got the solution
Ramesh Bugatha
+1  A: 

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

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);

Location location = locationManager.getLastKnownLocation(provider);

updateWithNewLocation(location);

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

}

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

public void onProviderDisabled(String provider){
  updateWithNewLocation(null);
}

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

};

Ramesh Bugatha