-1

I'm using the Google maps activity and the app crushes when I write gibberish and hit "search". However, it works perfectly with a real location. How can I prevent it from crashing?

My code:

public void onSearch(View view) {
    String location = locationTS.getText().toString();
    if (location != null || !location.equals("")) {
        Geocoder geocoder = new Geocoder(this);
        List<Address> addressList=null;
        try {
            addressList= geocoder.getFromLocationName(location, 1);
            mMap.clear();
        } 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("Marker"));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,80));
        latitudeB=latLng.latitude;
        longitudeB=latLng.longitude;
    }
    else {
        Toast.makeText(getApplicationContext(), "please fill in an available location",
                Toast.LENGTH_LONG).show();

    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Niv Peretz
  • 19
  • 3

2 Answers2

0

Add this check before getting lat and longitude in your code

address.hasLatitude() && address.hasLongitude()

Try this

 public void onSearch(View view) {
        String location = locationTS.getText().toString();
        if (location != null || !location.equals("")) {
            Geocoder geocoder = new Geocoder(this);
            List<Address> addressList=null;
            try {
                addressList= geocoder.getFromLocationName(location, 1);
                mMap.clear();
          Address address=addressList.get(0);
           // check if it has lat and long
           if(address.hasLatitude() && address.hasLongitude()){
                  LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude());
            mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,80));
            latitudeB=latLng.latitude;
            longitudeB=latLng.longitude;
                }  
            } catch (IOException e) {
                e.printStackTrace();
            }



        }
        else {
            Toast.makeText(getApplicationContext(), "please fill in an available location",
                    Toast.LENGTH_LONG).show();

        }
    }
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

From the documentation of the Geocoder.getFromLocationName method:

Returns null or empty list if no matches were found or there is no backend service available.

Thus, to solve your problem you can do:

if (addressList != null && !addressList.isEmpty()) {
    Address address=addressList.get(0);
    LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude());
    mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,80));
    latitudeB=latLng.latitude;
    longitudeB=latLng.longitude;
} else {
    Toast.makeText(getApplicationContext(), "No location found",
            Toast.LENGTH_LONG).show();
}
antonio
  • 18,044
  • 4
  • 45
  • 61