Google Maps API - Error Geocoder Timeout Exception

1

I have a method which is responsible for returning a URL with latitude and longitude according to the search term, the method is as follows:

public String getUrlFromTermsSearch(String searchTerm, Context context,String baseUrl){
    String url = "";

    try {
        Geocoder selected_place_geocoder = new Geocoder(context);
        List<Address> address;
        address = selected_place_geocoder.getFromLocationName(searchTerm,1);

        if (address.size()>0) {
            Address location = address.get(0);
            String urlF = baseUrl+searchTerm+"/"+location.getLatitude()+"/"+location.getLongitude()+"";
            url = urlF.replaceAll(" ","%20");
        }else{
            Log.d("error con Geocoder", ">>>: ");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return url;
}

But at times it shows or returns an exception in this part:

selected_place_geocoder.getFromLocationName(searchTerm,1);

Which is

  

error geocoder timeout exception

How could I solve this situation?

    
asked by devjav 24.01.2017 в 16:05
source

1 answer

0

This error has happened to me because at that moment I can not make the connection to the server, either because I am in a mobile network and I do not have enough signal or my internet is simply failing.

What do I recommend?

1) Put this whole method on a Asynk Task

2) Verify before at the time of making the request you have active internet.

Another solution:

Instead of using the geocoder, you can make a request to the following link:

http://maps.googleapis.com/maps/api/geocode/json?latlng=latitud,longitud&sensor=true

It will return a JSON with nearby addresses replacing latitude and longitude with the coordinates you need. For example: link

For more information on how to customize the petition, you can enter here .

    
answered by 24.01.2017 в 22:39