Android -可以在没有互联网连接的情况下使用NetworkProvider获取当前位置的经度和纬度



有任何方法获得当前位置的纬度和经度没有互联网连接(移动数据关闭和Wifi关闭),只使用NetworkProvider。我知道我们会得到之前追踪到的最后位置。但是,我需要得到更新的当前位置的纬度和经度

不上网也能得到经纬度

LocationManager lm;
lm = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
Location net_loc = null;
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//net_loc.getLatitude(); net_loc.getLongitude();

try this,

 try {
                LocationManager mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
                    LocationListner mListner = new LocationListner();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                try {
                                    mlocManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mListner);
                                } catch (Throwable e) {
                                    e.printStackTrace();
                                }
                                mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListner);
                            } catch (Throwable e) {
                                e.printStackTrace();
                            }
                            try {
                                mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mListner);
                            } catch (Throwable e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Throwable e) {
                    e.printStackTrace();
                }

public String getLatitude() {
        Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc == null) {
            loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (loc == null) {
                loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            }
            if (loc != null) {
                return "" + loc.getLatitude();
            }
        } else {
            return "" + loc.getLatitude();
        }
        return "0";
    }
public String getLongitude() {
        Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc == null) {
            loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (loc == null) {
                loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            }
            if (loc != null) {
                return "" + loc.getLongitude();
            }
        } else {
            return "" + loc.getLongitude();
        }
        return "0";
    }

最新更新