Android - 使用NetworkProvider获取当前位置而无需连接互联网可以获得经度和纬度吗?

时间:2014-05-23 07:16:57

标签: android

有任何方法可以仅使用NetworkProvider获取当前位置的纬度和经度而无需互联网连接(移动数据关闭和Wifi关闭)。我知道我们将获得之前跟踪的最后一个已知位置。但是,我需要更新当前位置的纬度和经度。

提前致谢。

2 个答案:

答案 0 :(得分:0)

试试这个,

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

答案 1 :(得分:0)

我们在没有互联网连接的情况下获得经纬度。

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();