[Android-Location]无法从LocationManager获取经度和纬度

时间:2014-05-23 07:21:50

标签: android locationmanager android-location

我使用LocationManager通过“经度”和“纬度”获取用户地址,但是当我调用location.getLongitude()和location.getLatitude()时,它返回0给我。我设法使用Google,但没有人遇到我的问题。

杰里米·爱德华兹先生建议我:

  

确保在手机设置中启用了基于网络的位置。您可以检测到这种情况,并在您确实需要时提示用户启用它。   我相信这是启动此活动的代码行。

我这样做但它可以提供帮助。有时候给我经度和纬度,有时候没有。

这是我的代码:

String locationProvider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager
            .getLastKnownLocation(locationProvider);

AndroidMainfest.xml中的权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

任何人都可以帮助我吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

您正尝试仅从网络提供商处获取。尝试使用以下代码。它可以帮助您从GPS提供商处获得,如果价值不可用,那么它将帮助您从网络提供商那里获得坐标

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute



        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 GPS Provider and no network provider is enabled
        } 
        else 
        {   // Either GPS provider or network provider is enabled

            // First get location from Network Provider
            if (isNetworkEnabled) 
            {
                locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) 
                {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) 
                    {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        this.canGetLocation = true;
                    }
                }
            }// End of IF network enabled

            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) 
            {
                locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) 
                {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) 
                    {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        this.canGetLocation = true;
                    }
                }

            }// End of if GPS Enabled
        }// End of Either GPS provider or network provider is enabled