Bruno Riba Barbosa
Bruno Riba Barbosa

Reputation: 41

Requesting nearest city in android

I need to calculate the near city in an android app using only the Network provider. I manage to do it with the following code but it turns the app very slow and it works only sometimes.



public Locator(final Context c) {

        LocationManager locationManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Geocoder gcd = new Geocoder(c, Locale.getDefault());
        List addresses;
        try {
            addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            if (addresses.size() > 0) {
                if(addresses.get(0).getLocality()!=null){
                    Locator.setCity(addresses.get(0).getLocality());
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }       
    }

Any ideas?

Thanks in advance.

Upvotes: 0

Views: 588

Answers (1)

Tai Dao
Tai Dao

Reputation: 3437

You can try Google Place API. Here is a example & tutorial

Upvotes: 2

Related Questions