2

When I search by country name, it works perfect, but when I search for the City or places, app crashes. Here is the code:

EditText location_tf = (EditText)findViewById(R.id.TFaddress);
        String location = location_tf.getText().toString();
        //Toast.makeText(this, location, Toast.LENGTH_SHORT).show();

        Geocoder gc = new Geocoder(this);
        List<Address> list = gc.getFromLocationName(location, 1);
        Address add = list.get(0);
        String locality = add.getLocality();
        Toast.makeText(this, locality, Toast.LENGTH_SHORT).show();

        double lat = add.getLatitude();
        double lng = add.getLongitude();

        gotoLocation(lat, lng, 4);
camelCaseCoder
  • 1,447
  • 19
  • 32
Khateeb321
  • 1,861
  • 23
  • 25

3 Answers3

3

thanks for the help, I found your answers help. My problem is solved, I was testing app on MIUI Chinese ROM, after installing MIUI international ROM, my code is working like a charm.

Thank you.

Khateeb321
  • 1,861
  • 23
  • 25
1

Based on the answers provided over this SO threads

I would say that, Geocoder doesn't always return a value. You need to try to send a request 3 times in a for loop. I should be able to return atleast once. If not then, their might be a connection issue or can be other issues like server dis not reply to your request.

You need to try something like below

try {
    List<Address> geoResults = geocoder.getFromLocationName("<address goes here>", 1);
    while (geoResults.size()==0) {
        geoResults = geocoder.getFromLocationName("<address goes here>", 1);
    }
    if (geoResults.size()>0) {
        Address addr = geoResults.get(0);
        myLocation.setLatitude(addr.getLatitude());
        myLocation.setLongitude(addr.getLongitude());
    }
} catch (Exception e) {
    System.out.print(e.getMessage());
}
Community
  • 1
  • 1
karan
  • 8,637
  • 3
  • 41
  • 78
0

To get the details of any city or country just use the api defined here . The api is basically - http://maps.googleapis.com/maps/api/geocode/json?address=<yourCityName>&sensor=false

Kaveesh Kanwal
  • 1,753
  • 17
  • 16