我正在尝试在移动设备上获取 GPS 纬度和经度。日志中没有错误,但我无法获得任何信息。无论是否选择了“使用无线网络/GPS”设置。需要帮助
public class MainActivity extends Activity {
private final static String TAG = "GPSTest";
private LocationManager mLocationManager;
private LocationListener mLocationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String serviceName = Context.LOCATION_SERVICE;
mLocationManager = (LocationManager) getSystemService(serviceName);
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.e(TAG, "onLocationChanged");
if (location != null) {
Log.e(TAG, "Current Latitude = " + location.getLatitude());
Log.e(TAG, "Current Longitude = " + location.getLongitude());
}
mLocationManager.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.e(TAG, "onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.e(TAG, "onProviderEnabled");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.e(TAG, "onStatusChanged");
}
};
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, mLocationListener);
} else {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, mLocationListener);
}
}
@Override
protected void onDestroy() {
mLocationManager.removeUpdates(mLocationListener);
super.onDestroy();
}
}