0

I have implemented this code to display address based on GPS. The latitude and longitude are working fine (appear on the screen) however, for the address its stands "no address found". Please have look at this code and point any errors. Thanks

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find__location);

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager =(LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);

    //String provider = LocationManager.GPS_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(location);

    locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);

    //buttons assigned
    Button mainMenuBtn = (Button) findViewById(id.mainMenuBtn);

    mainMenuBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            finish();


        }
    });
}

    private final LocationListener locationListener = new LocationListener() {

            @Override
    public void onLocationChanged(Location location) {
        updateWithNewLocation(location);

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        updateWithNewLocation(null);

    }


};

private void updateWithNewLocation(Location location) {
    String latLong;
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.myLocationText);

    String addressString = "no address found";

    if(location != null){
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        latLong = "Lat: "+lat+"\nLong: "+lng;

        //double latitude = location.getLatitude();
        //double longitude = location.getLongitude();


        Geocoder gc = new Geocoder(this,Locale.getDefault());
        try{

            List<Address> addresses = gc.getFromLocation(lat, lng, 1);
            StringBuilder sb = new StringBuilder();
            if (addresses.size() > 0){
                Address address = addresses.get(0);

                for(int i=0; i < address.getMaxAddressLineIndex(); i++)

                    sb.append(address.getAddressLine(i)).append("\n");
                    sb.append(address.getLocality()).append("\n");
                    sb.append(address.getPostalCode()).append("\n");
                    sb.append(address.getCountryName());
            }

              addressString = sb.toString();

        }catch (IOException e){}
    }else{
        latLong = "No location found";
    }

    myLocationText.setText("Your coordinates are:\n"+latLong + "\n"+addressString);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.find__location, menu);
    return true;
}

}

2 Answers2

0

I have had the same issue which was pretty annoying. Some answers suggested that geocoder does not always return a value so you have to loop some. However, this is not a good approach as you may never get a value, which sometimes is the case. Thus, what I have done in my project was to use google's reverse geocoding api. Idea is same but methodology is different.

This is the api page: https://developers.google.com/maps/documentation/geocoding/

Check the first answer here : Android Geocoder getFromLocationName always returns null Hope this helps

Community
  • 1
  • 1
Can Gokdere
  • 286
  • 3
  • 12
0

I prefer to get address using google geocoding API, by sending lat lon params, you'll get some informations https://developers.google.com/maps/documentation/geocoding/

Fahriyal Afif
  • 560
  • 3
  • 11