某些设备不返回当前纬度经度

时间:2015-05-19 16:29:45

标签: android google-maps location latitude-longitude

我正在尝试获取用户的地址,因为我需要用户的当前纬度和经度。我使用以下代码进行相同的

public LatLng getCurrentLocation(Context context) {
    try {
        LocationManager locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();

        String locProvider = locMgr.getBestProvider(criteria, false);
        Location location = locMgr.getLastKnownLocation(locProvider);

        // getting GPS status
        boolean isGPSEnabled = locMgr
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        boolean isNWEnabled = locMgr
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNWEnabled) {
            Log.v(TAG, "Network and GPS are unavailable");
            // no network provider is enabled
            return null;
        } else {
            // First get location from Network Provider
            if (isNWEnabled) {
                Log.v(TAG, "Network is enabled for getting the location");
                if (locMgr != null) {
                    Log.v(TAG,
                            "Location manager is not null so fetch the location from network");
                    location = null;
                    location = locMgr
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    if (locMgr != null)
                        location = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                }
            }
        }
        return new LatLng(location.getLatitude(), location.getLongitude());
    } catch (NullPointerException ne) {
        Log.e(TAG, "Current Lat Lng is Null");
        return new LatLng(0, 0);
    } catch (Exception e) {
        e.printStackTrace();
        return new LatLng(0, 0);
    }
}

在我使用android 4.4的Moto中,它工作得非常好但是在某些设备中它不会返回lat lng而我得到0,0这是在异常中返回的。 我需要改变以使其在所有设备中都正确。

0 个答案:

没有答案