简体   繁体   中英

Android Location returning null

Having some trouble trying to determine longitude and latitude, sometimes it works and sometimes it doesn't. Hopefully someone will be willing to take a look, i've spent days trying to figure this out.

Have this in my manifest permissions ACCESS_FINE_LOCATION

 private void comenzarLocalizacion()
    {

        locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        mostrarPosicion(loc);

        locListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                mostrarPosicion(location);
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }

        };

        locManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 30000, 0, locListener);
    }

    private void mostrarPosicion(Location loc) {
        if(loc != null)
        {
            lblLatitud.setText("Latitud: " + String.valueOf(loc.getLatitude()));
            lblLongitud.setText("Longitud: " + String.valueOf(loc.getLongitude()));
            lblPrecision.setText("Precision: " + String.valueOf(loc.getAccuracy()));
            Log.i("", String.valueOf(loc.getLatitude() + " - " + String.valueOf(loc.getLongitude())));


        }
        else
        {
            lblLatitud.setText("Latitud: (cannot be found)");
            lblLongitud.setText("Longitud: (cannot be found)");
            lblPrecision.setText("Precision: (cannot be found)");
        }
    }

#Precisely the best way to access location on is through (new) Fused Location Providers API

import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

public class Locations extends IntentService implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {


    public Locations() {
        super("Locations");
        Log.d("Locations", "Location service started... ");
    }

    private LocationRequest locationRequest;
    private LocationClient locationClient;
    private Location location;

    @Override
    protected void onHandleIntent(Intent intent) {
        locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationClient = new LocationClient(this, this, this);
        locationClient.connect();
    }

    @Override
    public void onLocationChanged(Location l) {
     // do something on Location change.
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {

    }

    @Override
    public void onConnected(Bundle arg0) {
        Log.w("Locations", "Location client connected...");

            // get last location
        location = locationClient.getLastLocation();
        Log.w("Locations", "Latitude : "+location.getLatitude() + "");
        Log.w("Locations", "Longitude : "+location.getLongitude() + "");
    }

    @Override
    public void onDestroy() {
        if (locationClient.isConnected()) {
            locationClient.removeLocationUpdates(this);
        }
        locationClient.disconnect();
    }

    @Override
    public void onDisconnected() {
    }

}

Source https://developer.android.com/training/location/index.html

我前段时间也遇到了同样的问题...如果GPS前段时间在设备中开启,则LocationManager.GPS_PROVIDER实际上可以工作,否则此代码无法正常工作,因此我决定借助LocationManager.NETWORK_PROVIDER来获取位置。可能会帮助您http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

Here I have given the code for getting the location. The code will first check the location via GPS . If GPS is not available it will use your network provider to get the coarse location. Try and let me know if it works.

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