user2813186
user2813186

Reputation: 3

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)");
        }
    }

Upvotes: 0

Views: 4310

Answers (3)

Rachit Mishra
Rachit Mishra

Reputation: 6112

#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

Upvotes: 1

Naddy
Naddy

Reputation: 2674

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.

Upvotes: 0

Nikhil
Nikhil

Reputation: 1043

i am also face the same problem some time ago... actually LocationManager.GPS_PROVIDER work if GPS is on in device few time ago otherwise this code not work so i am decided to get location with the help of LocationManager.NETWORK_PROVIDER..... may be it will help you http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

Upvotes: 0

Related Questions