简体   繁体   中英

[Android-Location]Can not get Longitude and Latitude from LocationManager

I used LocationManager to get user address by "Longitude" and "Latitude" but when i call location.getLongitude() and location.getLatitude(), it return 0 back to me. I manage to Google but have nobody meet my issue.

Mr.Jeremy Edwards suggest me that:

Make sure that network based location is enabled in the phone's settings. You can detect this situation and prompt the user to enable it if you really need it. I believe this is the line of code to launch this activity.

I do that but it can help. Sometime that give me Longitude and Latitude, some time not.

Here is my code:

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

The permission in the 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"/>

Can anyone help me? Thank you in advance!

You are trying to get only from network provider. Try using below code. It will help you get from GPS Provider and if value not available, then it will help you get coordinates from network provider

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM