0

I am working on GPS system for android. I am able to find the latitude and longitude of my current position, but when I try to convert my latitude and longitude into Physical address my code doesn't work. I have tried several possibilities. I have posted my code here.

Any help is much appreciated. Thanks.

try {

        List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
        System.out.println(""+latitude);
        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 {
        latLongString = "No location found";
        }
        myLocationText.setText("Your Current Position is:\n" +
        latLongString + "\n" + addressString);
        }

My problem is, I couldn't get any value in the List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); . It returns a empty list only. Could anyone tell me what the problem is and how to sort it out...

Andro Selva
  • 53,910
  • 52
  • 193
  • 240

2 Answers2

0

If the GeoCoder returns an empty list, you need to check if you have a proper GeoCoder implementation available on the device (emulator or real phone).

This can be done using the isPresent() method on the Geocoder object.

http://developer.android.com/reference/android/location/Geocoder.html

Also, when running on an emulator, make sure your AVD image is setup with the Google APIs.

ddewaele
  • 22,363
  • 10
  • 69
  • 82
0
Geocoder gc = new Geocoder(ctx, Locale.getDefault());
List<Address> addresses;
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(",");
}

This would get a location. But sometimes an exception comes as "Unable to parse the response from server". You need to handle the exception.Also check for the INTERNET permission in the manifest.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Vansi
  • 46
  • 6
  • Hi there.. I have tried this already. I was actually asking for a different approach if exists. – Andro Selva Apr 26 '11 at 09:58
  • then u can probably call a google webservice and handle the result – Vansi Apr 26 '11 at 11:28
  • webservice like this.... http://maps.google.com/maps/geo?q=lattitude,longitude&output=csv&sensor=false – Vansi Apr 26 '11 at 11:29
  • Then handle the result. String url = "http://maps.google.com/maps/geo?q="+lattitude+","+longitude+"&output=csv&sensor=false"; Uri.parse(url); HttpClient mClient= new DefaultHttpClient(); try { HttpResponse res = mClient.execute(get); String msg = EntityUtils.toString(res.getEntity()); String[] addressInfo = msg.split(","); msg =""; for (int i = 2; i < addressInfo.length; i++) { msg = msg+addressInfo[i]; } msg = "Location: " + msg ; The string msg here will contain the address. – Vansi Apr 26 '11 at 11:33
  • Great idea.. Thanks there.. I will try to use it and will let know how it works. – Andro Selva Apr 27 '11 at 04:34