Reputation: 704
I am writing a program that gets the complete location from google maps, I managed to get latitude and longitude, but when it comes to address, I did everything as mentioned in different websites but I get an empty List.
Here is the code :
This is the function that saves the address in a list
public List<Address> getAddress() {
try {
Geocoder geocoder;
List<Address> addresses;
double latitude, longitude;
latitude = Double.parseDouble(lat);
longitude = Double.parseDouble(lng);
System.out.println(latitude + " / " + longitude);
geocoder = new Geocoder(this);
if (latitude != 0 || longitude != 0) {
addresses = geocoder.getFromLocation(latitude ,
longitude, 2);
return addresses;
} else {
Toast.makeText(this, "latitude and longitude are null",
Toast.LENGTH_LONG).show();
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
And here is the function that calls it :
@Override
public void onLocationChanged(Location location) {
lng = location.getLongitude() + "";
lat = location.getLatitude() + "";
try
{
getAddress();
}
catch (Exception e)
{
System.out.println("Can't Call getAddress()");
}
gotoLocation(location);
}
This is the given error each time the onLocationChanged() is called
06-16 12:06:23.595: W/System.err(28123): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 06-16 12:06:23.595: W/System.err(28123): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 06-16 12:06:23.595: W/System.err(28123): at java.util.ArrayList.get(ArrayList.java:308) 06-16 12:06:23.595: W/System.err(28123): at com.MOS.fastfood.MapActivity.getAddress(MapActivity.java:559) 06-16 12:06:23.595: W/System.err(28123): at com.MOS.fastfood.MapActivity.onLocationChanged(MapActivity.java:522) 06-16 12:06:23.595: W/System.err(28123): at com.google.android.gms.internal.zzpe$zza.handleMessage(Unknown Source) 06-16 12:06:23.595: W/System.err(28123): at android.os.Handler.dispatchMessage(Handler.java:99) 06-16 12:06:23.600: W/System.err(28123): at android.os.Looper.loop(Looper.java:176) 06-16 12:06:23.600: W/System.err(28123): at android.app.ActivityThread.main(ActivityThread.java:5419) 06-16 12:06:23.600: W/System.err(28123): at java.lang.reflect.Method.invokeNative(Native Method) 06-16 12:06:23.600: W/System.err(28123): at java.lang.reflect.Method.invoke(Method.java:525) 06-16 12:06:23.600: W/System.err(28123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 06-16 12:06:23.600: W/System.err(28123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 06-16 12:06:23.600: W/System.err(28123): at dalvik.system.NativeStart.main(Native Method)
This is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MOS.fastfood"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
<!-- added permission manually to reveice MAP -->
<permission
android:name="com.MOS.fastfood.permission.MAPS_RECEIVE"
android:protectionLevel="signature" >
</permission>
<!-- permissions to access internet -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- important permissions to fetch map from google -->
<uses-permission android:name="com.MOS.fastfood.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- only devices that support version 2.0 of openGL can download, for graphics -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- two permissions used to determine the current location using network towers and gps -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- ADDING THE DEBUGGING KEY -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="***" />
<!-- THIS META DATA ALLOWS "ISGOOGLEPLAYSERVICES ENABLED" TO FUNCTION WITHOUT CRASH -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".AddRestaurantActivity"
android:label="@string/title_activity_add_restaurant" >
</activity>
<activity
android:name=".FacebookLoginActivity"
android:label="@string/title_activity_facebook_login" >
</activity>
<activity
android:name=".ListActivity"
android:label="@string/title_activity_list" >
</activity>
<activity
android:name=".MapActivity"
android:label="@string/title_activity_map" >
</activity>
<activity
android:name=".RestaurantInfoActivity"
android:label="@string/title_activity_rate" >
</activity>
<activity
android:name=".Loader"
android:label="@string/title_activity_loader" >
</activity>
</application>
</manifest>
Upvotes: 1
Views: 1544
Reputation: 13368
Try this code
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
Upvotes: 1