problem in getting gps coordinates in android 2.1(Found Answer)
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
please 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" /&g开发者_开发知识库t;
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
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){ }
};
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.
Hi I am also facing same problem in 2.1 after i resolved like this... it will work for you
myManager = ((LocationManager) ApplicationController.getAppContext()
.getSystemService(Context.LOCATION_SERVICE));
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1 * 1000, 0.00001f, this);
Follow location changed asy method in tha u will get lat/and long
don't use bundle object over there it wont work instead of that you can use Gps Satellite class for 2.1..
精彩评论