Rick
Rick

Reputation: 4013

Why can't I retrieve my position?

In my project I'm trying to get the latitude, the longitude and the address but for some reason I can't get it appears the message Location not available everytime.

In the manifest.xml

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

Have someone had this issue before? This is my class:

public class GEO extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private TextView addressField; //Add a new TextView to your activity_main to display the address
private LocationManager locationManager;
private String provider;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.geo);
    latituteField = (TextView) findViewById(R.id.txt1);
    longitudeField = (TextView) findViewById(R.id.txt2);
    addressField = (TextView) findViewById(R.id.txt3);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, true);
    Log.d("SomeTag", provider);
    Log.d("SomeTag", String.valueOf(locationManager.isProviderEnabled(provider)));
    Location location = locationManager.getLastKnownLocation(provider);
    String locationProvider = LocationManager.GPS_PROVIDER;

    Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

    if (lastKnownLocation != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(lastKnownLocation);
    } else {
        latituteField.setText("Location not available");
        longitudeField.setText("Location not available");
        addressField.setText("Location not available");
    }
}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    //You had this as int. It is advised to have Lat/Loing as double.
    double lat = location.getLatitude();
    double lng = location.getLongitude();

    Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
    StringBuilder builder = new StringBuilder();
    try {
        List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
        int maxLines = address.get(0).getMaxAddressLineIndex();
        for (int i=0; i<maxLines; i++) {
            String addressStr = address.get(0).getAddressLine(i);
            builder.append(addressStr);
            builder.append(" ");
        }

        String fnialAddress = builder.toString(); //This is the complete address.

        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
        addressField.setText(fnialAddress); //This will display the final address.

    } catch (IOException e) {}
    catch (NullPointerException e) {}
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}

I do run my project with a phone

Upvotes: 0

Views: 100

Answers (1)

puj
puj

Reputation: 770

It could be that address.get(0) is null in

    List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
    int maxLines = address.get(0).getMaxAddressLineIndex();

And you are catching the NPEs, so it might be difficult to identify exactly why you are not getting the expected results.

Something like this might help:

    try {
        List<Address> addresses = geoCoder.getFromLocation(lat, lng, 1);
        Address firstAddress = addresses.get(0);
        if(firstAddress != null) {
            int maxLines = firstAddress.getMaxAddressLineIndex();
            for (int i = 0; i < maxLines; i++) {
                String addressStr = firstAddress.getAddressLine(i);
                builder.append(addressStr);
                builder.append(" ");
            }
            addressField.setText(builder.toString()); //This will display the final address.
        }

        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));

    } catch (IOException e) {
        e.printStackTrace();
    }

Also, from http://developer.android.com/reference/android/location/Geocoder.html

Try checking isPresent

public static boolean isPresent ()

Added in API level 9
Returns true if the Geocoder methods getFromLocation and getFromLocationName are implemented. Lack of network connectivity may still cause these methods to return null or empty lists.

Upvotes: 1

Related Questions