Android: Not able to get location name from Google Geocoding API

1.9k views Asked by At

I'm trying to get location name from Latitude and Longitude using google reverse Geocoding API. and I'm getting error.

Here is Code I'm using :

  Geocoder geocoder= new Geocoder(MainActivity.this, Locale.ENGLISH);
         
        try {
               
              //Place your latitude and longitude
              List<Address> addresses = geocoder.getFromLocation(37.423247,-122.085469, 1);
              
              TextView myAddress=(TextView)findViewById(R.id.textView1);
    if(addresses != null) {
               
                  Address fetchedAddress = addresses.get(0);
                  StringBuilder strAddress = new StringBuilder();
                
                  for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) {
                        strAddress.append(fetchedAddress.getAddressLine(i)).append("\n");
                  }
                
                  myAddress.setText("I am at: " +strAddress.toString());
               
              }
               
              else
                  myAddress.setText("No location found..!");
          
        } 
        catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 Toast.makeText(MainActivity.this,"Could not get address..!", Toast.LENGTH_LONG).show();
        }

Log file

 FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.get_mobiile_location/com.example.get_mobiile_location.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
      at android.app.ActivityThread.access$600(ActivityThread.java:141)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:5103)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:525)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
      at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
      at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
      at java.util.ArrayList.get(ArrayList.java:308)
      at com.example.get_mobiile_location.MainActivity.getMyLocation(MainActivity.java:79)
      at com.example.get_mobiile_location.MainActivity.onCreate(MainActivity.java:53)
      at android.app.Activity.performCreate(Activity.java:5133)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
      ... 11 more

Please check what I'm doing wrong.

1

There are 1 answers

3
Nagaraju V On

Based on the log

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

In the below line you are getting 0 addresses for the given lat,long values

List<Address> addresses = geocoder.getFromLocation(37.423247,-122.085469, 1);

So to avoid the crash check the condition before accessing data from it as follows

if(addresses != null&&addresses.size()>0) {
  //fetch data from addresses
}else{
        //display Toast message
     }

UPDATE :

And add following permissions if you forgot anyone

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.INTERNET" />

Refer following tutorial

http://karanbalkar.com/2013/11/tutorial-63-implement-reverse-geocoding-in-android/

Hope this will helps you.