2012-10-11 55 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不到一分钟