I 'm trying to use google maps. I have this code:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
List <Address> addressList = null;
if (location != null || !location.equals("")){
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
}catch (IOException e){
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title(location));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
}
}
But I receive this: addressList size 0. Error "java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0" location is my variable but works fine. what am I doing wrong? Thanks a lot.
Possibilities of getting Zero result are
List getFromLocationName (String locationName, int maxResults) : returns : a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
And before calling any method of Geocoder to get address or results, check the Geocoder service can be implemented using Geocoder.isPresent()
if(Geocoder.isPresent()){
//do your stuff
}else{
//cann't implement geocoder
}