如何在设备上启用位置服务的情况下获取最后一个位置?

时间:2015-03-25 08:18:41

标签: android

我希望在设备上启用位置服务时获取最后一个位置(以任何方式),这可能吗?
我该怎么办?

3 个答案:

答案 0 :(得分:0)

有三种方法可以找到您的位置refer this

TelephonyManager telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);   GsmCellLocation cellLocation =(GsmCellLocation)telephonyManager.getCellLocation();

int cellid = cellLocation.getCid();   int celllac = cellLocation.getLac();

答案 1 :(得分:0)

您可以在方法getLastKnownLocation中使用NETWORK_PROVIDER作为服务参数。

答案 2 :(得分:0)

如果您想获得没有GPS的位置,那么您应该使用NETWORK_PROVIDER来获取位置:

我从AndroidHive website获取了这个模块你可以在那里更多地参考。

您的查询将使用此方法getLocation()

解决
public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}