-2

I attempting to write an android app that that shows the users current address(reverse geolocation ) and although the code i have puts the marker at their current location it will not give their current address. I am not sure what i missing and would really appreciate it someone could point me in the right direction.

public class location extends Activity {

public GoogleMap map;
public Marker myLocation;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

        if(myLocation!=null)
           myLocation.remove();

        myLocation=map.addMarker(new MarkerOptions().position(latlng).icon(BitmapDescriptorFactory.defaultMarker(
                BitmapDescriptorFactory.HUE_MAGENTA)).title("Your Current Location."));

        double lat = location.getLatitude();
        double lng = location.getLongitude();
        latLongString = "Lat:" + lat + "\nLong:" + lng;

        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        Geocoder coder = new Geocoder(this, Locale.getDefault());

        if (!Geocoder.isPresent())
            addressString = "No geocoder available";
        else {
            try {
                List<Address> addresses = coder.getFromLocation(latitude, longitude, 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) {

            }
        }
    }

    myLocationText.setText("Your Current Position is:\n" +
            latLongString + "\n\n" + addressString);
}

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status,
                                Bundle extras) {}
};

}

1 Answer 1

0

Using Geocoder is the right approach. Do not swallow Exceptions like this, at least print it out:

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

Have a look into your logs, there might be an exception thrown while geocoding.

5
  • @sulia There is no exceptions and the code runs fine it just does not print the current address. Apr 28, 2014 at 17:25
  • Did you try to use the debugger of your ide to find the bug? What's the result of coder.getFromLocation(...) in your case?
    – sulai
    Apr 28, 2014 at 17:29
  • when the code is run it prints the statement You Current location and places a marker but does not print the long / lat or address. Apr 28, 2014 at 17:34
  • @user3493849 what? That doesn't even make sense. Place a debug break point where you set your strings and where you set your location text and see what your values actually are. Nowhere are you able to set your sstrings to "" so that is weird.
    – zgc7009
    Apr 28, 2014 at 18:08
  • @zg7009 the marker had a title called your current location and it appears on screen but the lon/lat and address that should be in the text view boxes dont appear Apr 28, 2014 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.