написал класс
Код: Выделить всё
public class GeoLocation {
String LOG_TAG = "GeoLocation";
private LocationManager manager;
Context mContext;
private Geocoder geocoder;
private LocationListener listener = new LocationListener() {
public void onLocationChanged(Location argLocation) {
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
public GeoLocation(Context ApplicationContext) {
Log.d(LOG_TAG, "GeoLocation create");
mContext = ApplicationContext;
}
private String printLocation(Location loc) {
String text = null;
if (loc != null) {
try {
Log.d(LOG_TAG, loc.toString());
List<Address> list = geocoder.getFromLocation(
loc.getLatitude(), loc.getLongitude(), 1);
Log.d(LOG_TAG, String.valueOf(list.size()));
Address address = list.get(0);
text = address.getLocality();
Log.d(LOG_TAG, " Locality:" + text);
} catch (IOException e) {
Log.d(LOG_TAG, e.getMessage());
}
} else {
text = "NAN";
}
return text;
}
public String GetCurrentCity() {
Log.d(LOG_TAG, "GGetCurrentCity");
String city = "";
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(true);
manager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
String BestProvider = manager.getBestProvider(criteria, true);
if (BestProvider != null) {
Log.d(LOG_TAG, BestProvider);
manager.requestLocationUpdates(BestProvider, 0, 0, listener);
Location loc = manager.getLastKnownLocation(BestProvider);
geocoder = new Geocoder(mContext);
city = printLocation(loc);
} else {
city = null;
}
return city;
}
}