2012-10-11 98 views
0

我已經寫了代碼,如果GPS被禁用它將啓用代碼並嘗試從GPS獲取位置,但我得到一個空值。下面是我的代碼清單文件Android GPS返回null位置

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

public void getValue() { 
    LocationManager mlocManager = (LocationManager) MySettings.this.getSystemService(Context.LOCATION_SERVICE); 
    boolean gpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    System.out.println("GPS IS "+gpsEnabled); 
    if (!gpsEnabled) { 
     String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 

     if (!provider.contains("gps")) { // if gps is disabled 
      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); 
     } 
    } 
    SimpleDateFormat sdfDate = new SimpleDateFormat("MM/dd/yyyy"); 
    try { 
     getBatteryLevel(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, MySettings.this); 
     Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     mLocation = location; 
     if (location != null) { 
      lat = location.getLatitude(); 
      lon = location.getLongitude(); 
      address = getAddress(); 
      alt = location.getAltitude(); 
      if (meterFootFlag) { 
       diameter = location.getAccuracy(); 
      } else 
       diameter = location.getAccuracy()/3.28084; 
     } else { 
      lat = 0.0; 
      lon = 0.0; 
      alt = 0.0; 
     } 

    } catch (Exception e) { 
     lat = 0.0; 
     lon = 0.0; 
     alt = 0.0; 
    } 

我也有添加權限,但我得到該位置的空值。

關於如何獲取位置的任何想法?

+1

查看更多這個http://stackoverflow.com/a/1609593/1321873 – Rajesh

回答

0

嘗試:

public Location showLocation(){ 
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    //Class model, save latitude and longitude 
    NavegadorCoordenadas locate = new NavegadorCoordenadas(); 
    Criteria crit = new Criteria(); 
    crit.setAccuracy(Criteria.ACCURACY_FINE); 
    String provider = lm.getBestProvider(crit, false); 
    Location loc = getLastKnownLocation(lm);      
    locate.setLatitude(loc.getLatitude()); 
    locate.setLongitude(loc.getLongitude()); 
    return loc;  
} 

private Location getLastKnownLocation(LocationManager location) { 
    List<String> providers = location.getProviders(true); 
    Location bestLocation = null; 
    for (String provider : providers) { 
     Location l = location.getLastKnownLocation(provider);   
     if (l == null) { 
      continue; 
     } 
     if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {    
      bestLocation = l; 
     } 
    } 
    if (bestLocation == null) { 
     return null; 
    } 
    return bestLocation; 
} 
0

確保您正在覈實DEVICE只。在仿真器會因爲它是在系統中運行,因此不列入有權限GPS

0
給空值 GPS
locationMangaer = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     if(!provider.contains("gps")){ //if gps is disabled 
      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); 
     } 

    locationListener = new MyLocationListener(); 
    locationMangaer.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10, 
      locationListener); 

現在使MyLocationListener類在同一活動。

private class MyLocationListener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location loc) { 

      String longitude = "Longitude: " +loc.getLongitude(); 
      String latitude = "Latitude: " +loc.getLatitude(); 

      /*----------to get City-Name from coordinates ------------- */ 
      String cityName=null;     
      Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());     
      List<Address> addresses; 
      try { 
      addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); 
      if (addresses.size() > 0) { 
       System.out.println(addresses.get(0).getLocality()); 
      cityName=addresses.get(0).getLocality(); 
       System.out.println("MESSAGE:"+cityName); 
      } 
      } catch (IOException e) {     
      e.printStackTrace(); 
      } 

      String s = longitude+"\n"+latitude+"\t city name:"+cityName; 
       Log.v("OUTPUT, s); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub   
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub   
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub   
    } 
} 

實際設備運行應用程序。

你可以看出,自動啓動GPS和看到控制檯 logcat。

2

你的代碼是正確的,只需等到全球定位系統從衛星獲得高度,它可能需要超過1分鐘。

0

確保通過 設置打開權限 - >應用程序 - > YourApp - >權限 而另一個原因可能是延遲,它需要時間來連接到網絡,somethimes不到一分鐘