2013-01-21 28 views
0

我想在我的應用程序中獲取當前位置(經度和緯度值)。 我用下面的代碼。Gps提供商正在返回空位置

問題是----------------->

When the Best provider is GPS,Location returned is null. 

我學到了一些SO職位,我添加LocationListener的我的代碼,那麼它的工作,但是當我重新啓動我的設備,然後再次運行該應用程序,當Provider是GPS時,它會提供相同的NULL值。我不明白爲什麼現在不工作。

我還了解到,全球定位系統供應商在獲取值慢,所以我必須添加LocationListener的解決這個issue.So,我已將此添加到我的代碼

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

爲什麼onLocationChanged中的代碼註釋掉了?這是當GSP或網絡向您發送新位置時將會調用的內容。同樣,當你使用GSP和網絡時,不需要調用getBestProvider。 –

回答

0

嘛餘噸裏德下面的代碼,它獲取的緯度和經度:

公共類MainActivity擴展活動 {

@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"/> 
相關問題