Android GPS正在返回null位置

时间:2012-10-11 13:07:30

标签: android gps

我已编写代码,如果禁用GPS,它将通过代码启用并尝试从gps获取位置但我得到一个空值。以下是我的代码

   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;
    }

此外,我已在清单文件中添加了权限

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

但是我得到了该位置的空值。

关于如何获取位置的任何想法?

5 个答案:

答案 0 :(得分:2)

你的代码是正确的,只要等到GP从卫星获得高度,它可能需要花费1分钟。

答案 1 :(得分: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;
}

答案 2 :(得分:0)

确保您仅检查DEVICE ..在模拟器中,它将为系统中运行的GPS提供空值,因此它不具有GPS <的权限/ p>

答案 3 :(得分:0)

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。

答案 4 :(得分:0)

确保通过该权限打开权限 设置 - &GT;应用程序 - &GT; YourApp - &gt;权限 另一个原因可能是延迟,连接网络需要一些时间,超过一分钟