Reputation: 41
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