0

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??

Khaled Mohamed
  • 217
  • 1
  • 7
  • 15

3 Answers3

2

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.

Community
  • 1
  • 1
Diego Frehner
  • 2,396
  • 1
  • 28
  • 35
1

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

thebmags
  • 24
  • 4
0

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);
Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57