我想在我的应用程序中获取当前位置(纬度和经度值)。 我使用了以下代码。
问题是----------------->
When the Best provider is GPS,Location returned is null.
我学到了一些SO帖子,我将LocationListener添加到我的代码中,然后它工作了但是当我重新启动设备并再次运行应用程序时,它在Provider为GPS时提供相同的NULL值。我不明白为什么它现在不起作用。
我还了解到Gps提供程序获取值的速度很慢,因此我必须添加LocationListener来解决此问题。所以,我将此添加到我的代码中
LocationListener locationListener = new MyLocationListener();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, locationListener);
此处完整代码----------->
public void getCurrentlocation()
{
String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.e("provider"," "+providers);
if(!providers.contains("gps")){
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
Log.e("your gps is","enabled");
}
LocationListener locationListener = new MyLocationListener();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 35000, 10, locationListener);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria,true);
Log.e("location provider",""+provider);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null)
{
System.out.println("Provider " + provider + " has been selected.");
strLatitude=String.valueOf(location.getLatitude());
strLongitude=String.valueOf(location.getLongitude());
Log.e("lattitude",strLatitude);
Log.e("logntitude",strLongitude);
latituteField.setText( strLatitude);
longitudeField.setText(strLongitude);
}
else
{
strLatitude=String.valueOf(0);
strLongitude=String.valueOf(0);
Log.e("lattitude",strLatitude);
Log.e("logntitude",strLongitude);
latituteField.setText( strLatitude);
longitudeField.setText(strLongitude);
}
}
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location)
{
// mLattitude = location.getLatitude();
// mLongitude = location.getLongitude();
//
// Log.w("value : ",
// String.valueOf(location.getLatitude()) + String.valueOf(location.getLongitude())
// +
//
//
// context.sendBroadcast(new Intent(Constants.BRDCAST_LOCATION_UPDATED));
}
@Override
public void onProviderDisabled(String provider)
{
// Log.w("provider", provider);
}
@Override
public void onProviderEnabled(String provider)
{
// Log.w("provider", provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
// Log.w("status", String.valueOf(status));
}
}
请帮助
提前致谢
答案 0 :(得分:0)
好吧,我尝试了下面的代码,它取得了纬度和经度:
公共类MainActivity扩展了Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/ *向后循环数组,如果你得到一个准确的位置,那么就打破循环* /
Location location = null;
for (int i=providers.size()-1; i>=0; i--) {
location = lm.getLastKnownLocation(providers.get(i));
if (location != null) break;
}
String Text = "My current location is: " +
"Latitud = " + location.getLatitude() +
"Longitud = " + location.getLongitude();
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
的AndroidManifest.xml:
请参阅设置以下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>