I'm trying to get the user's current location via GPS capability,
Wrote a simple class that implements LocationListener
public class LocationManagerHelper implements LocationListener {
private static double latitude;
private static double longitude;
@Override
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public static double getLatitude() {
return latitude;
}
public static double getLongitude() {
return longitude;
}
}
and from a simple Action I'm accessing these longitude and latitude values
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** create a TextView and write Hello World! */
TextView tv = new TextView(this);
LocationManager mlocManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new LocationManagerHelper();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
+ '
');
tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
+ '
');
} else {
tv.setText("GPS is not turned on...");
}
/** set the content view to the TextView */
setContentView(tv);
But it always returns 0.0 as the result.Couldn't figure out the problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…