I am making a register of people with address to show on the map where a person lives. When I type the correct address everything works normally, but when I get the error, which is not valid for Google Maps , the app closes.
@Override
public void onResume() {
super.onResume();
FragmentActivity context = getActivity();
LatLng local = new Localizador(context).getCoordenada("R. Eurita, 47 - Santa Teresa - Belo Horizonte - MG");
centralizaNo(local);
AlunoDAO dao = new AlunoDAO(context);
List<Aluno> alunos = dao.getLista();
for (Aluno aluno : alunos) {
GoogleMap map = getMap();
LatLng localAluno = new Localizador(context).getCoordenada(aluno.getEndereco());
MarkerOptions options = new MarkerOptions().title(aluno.getNome()).position(localAluno);
map.addMarker(options );
}
dao.close();
}
public void centralizaNo(LatLng local) {
GoogleMap map = getMap();
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(local , 15);
map.animateCamera(update );
}
I would like some help to handle the error and if the person enters an invalid address does not appear on the map to flow normally.
public class Localizador {
private Context context;
public Localizador(Context context){
this.context = context;
}
public LatLng getCoordenada(String endereco) {
Geocoder geocoder = new Geocoder(context);
try {
List<Address> enderecos = geocoder.getFromLocationName(endereco, 1);
if (!enderecos.isEmpty()){
Address enderecoLocalizado = enderecos.get(0);
double latitude = enderecoLocalizado.getLatitude();
double longitude = enderecoLocalizado.getLongitude();
return new LatLng(latitude, longitude);
} else {
return null;
}
} catch (IOException e) {
return null;
}
}
}