1
給空位置我試圖獲取當前位置在android系統的,我已經使用了Android和下面是我的代碼位置經理的android
locationManager = (LocationManager) getSystemService(this.context.LOCATION_SERVICE);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(true);
crta.setBearingRequired(true);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
Log.d("","provider : "+provider);
provider = LocationManager.GPS_PROVIDER;
Log.d("","provider : "+provider);
locationManager.requestLocationUpdates(provider, 0, 0, (LocationListener) this);
Location location = locationManager.getLastKnownLocation(provider);
//isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (location != null)
{
double latitude = location.getLatitude();
double longitude = location.getLongitude();
txtLat.setText("Latitude"+latitude+"longitude"+longitude);
Log.d("not null", "location is not null");
}
else
{
Toast.makeText(getApplicationContext(), "locatyion is null", Toast.LENGTH_LONG).show();
Log.d("null","location is null");
}
的問題是,它不給我我的當前位置當我打開應用程序的位置爲空。誰能幫忙?我看過很多例子,也嘗試了其他例子,但沒有奏效。 我已經加入
if (!isNetworkEnabled)
{
Toast.makeText(getApplicationContext(), "No network provider the gps wont work", Toast.LENGTH_LONG).show();
// no network provider is enabled
}
else{
Toast.makeText(getApplicationContext(), "Network is available", Toast.LENGTH_LONG).show();
}
其說GPS可用和網絡不可用