【问题标题】:Location manager give null location in android [duplicate]位置管理器在android中给出空位置[重复]
【发布时间】:2014-01-12 13:11:44
【问题描述】:

我正在尝试获取 android 中的当前位置,因为我使用了 android,以下是我的代码

locationManager = (LocationManager) getSystemService(this.context.LOCATION_SERVICE);
 Criteria crta = new Criteria();
     crta.setAccuracy(Criteria.ACCURACY_FINE);
     crta.setAltitudeRequired(true);
     crta.setBearingRequired(true);
     crta.setCostAllowed(true);
     crta.setPowerRequirement(Criteria.POWER_LOW); 
     String provider = locationManager.getBestProvider(crta, true);
     Log.d("","provider : "+provider);
     provider = LocationManager.GPS_PROVIDER; 
     Log.d("","provider : "+provider);
     locationManager.requestLocationUpdates(provider, 0, 0, (LocationListener) this);
     Location location = locationManager.getLastKnownLocation(provider); 
     //isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (location != null)
     {
       double  latitude = location.getLatitude();
       double  longitude = location.getLongitude();
       txtLat.setText("Latitude"+latitude+"longitude"+longitude);
       Log.d("not null", "location is not null");
     }
    else
    {

      Toast.makeText(getApplicationContext(), "locatyion is null", Toast.LENGTH_LONG).show();
      Log.d("null","location is null"); 
    }

问题是,当我打开应用程序时,它没有给我当前位置,该位置为空。任何人都可以帮忙吗?我看过很多例子,也尝试过其他例子,但没有奏效。 我已经添加了

if (!isNetworkEnabled)
    {
     Toast.makeText(getApplicationContext(), "No network provider the gps wont work", Toast.LENGTH_LONG).show();
        // no network provider is enabled
    }
    else{

        Toast.makeText(getApplicationContext(), "Network is available", Toast.LENGTH_LONG).show();
    }

它说gps可用但网络不可用

【问题讨论】:

    标签: android locationmanager


    【解决方案1】:

    代码需要与位置改变监听器的广播接收器异步...

    发生这种情况是因为 GPS 需要一些时间来提供位置,当它第一次启动时,一切都是 null

    请看下面的代码:

    public class Example extends Activity implements LocationListener {
        LocationManager mLocationManager;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    
            Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
                // Do something with the recent location fix 
                //  if it is less than two minutes old,
                //  otherwise wait for the update below
            }
        }
    
        public void onLocationChanged(Location location) {
            if (location != null) {
                Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
                // You need to call this whenever you are done:
                // mLocationManager.removeUpdates(this);
            }
        }
    
        // Required functions    
        public void onProviderDisabled(String arg0) {}
        public void onProviderEnabled(String arg0) {}
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
    }
    

    更多here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多