Khaled Mohamed
Khaled Mohamed

Reputation: 217

Reverse address using Geocoder

I am trying to get an address for a specific geopoint but when i try it using try and catch it fails.Here it is my code

 srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6)); 

        Geocoder geocoder=new Geocoder(getBaseContext(), Locale.getDefault());
        try
        {
            List<Address> addresses=geocoder.getFromLocation(srcGeoPoint.getLatitudeE6()/1E6,
                    srcGeoPoint.getLongitudeE6()/1E6,1);
            String add="";
            if(addresses.size()>0)
            {
                for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                        i++)
                      add += addresses.get(0).getAddressLine(i) + "\n";
            }
            tv.setText(add);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

It fails on this line List addresses=geocoder.getFromLocation(srcGeoPoint.getLatitudeE6()/1E6, srcGeoPoint.getLongitudeE6()/1E6,1); any help??

Upvotes: 0

Views: 452

Answers (3)

thebmags
thebmags

Reputation: 24

I don't have the power to approve answers yet but Diego is 100% correct, geocoder does not work on the emulators you have to use a real device if you want to debug it. Trust me, it took the longest time banging my head up against a wall until I found out what was wrong. So do yourself a favor so you dont get a concussion, just use a real device

Upvotes: 1

Hesham Saeed
Hesham Saeed

Reputation: 5378

You would cast these to doubles:

srcGeoPoint.getLatitudeE6()/1E6
srcGeoPoint.getLongitudeE6()/1E6

full code:

List addresses=geocoder.getFromLocation((double) (srcGeoPoint.getLatitudeE6()/1E6), (double) (srcGeoPoint.getLongitudeE6()/1E6),1);

Upvotes: 0

Diego Frehner
Diego Frehner

Reputation: 2570

The geocoder does not work in the emulator as described more here: Does geocoder.getFromLocationName not work on emulators?

I also implemented myself and it really does not in the emulator but does on a device.

Upvotes: 2

Related Questions