Как по координатам определить город в Андроид?
Здравствуйте! Есть 2 кода. Первый определяет координаты. Второй по вписанным вручную координатам определяет адреса. Как объединить эти 2 кода, чтобы получить приложение, в котором по координатам первого кода, можно было получить название города из второго кода? Главная проблема похоже в том, что в первом коде координаты типа String, а во втором координаты типа Double. Пробовал String перевести в Double. Не получается.
Вот первый код:
public class MainActivity extends Activity {
private LocationManager mLocationManager;
private LocationListener mLocationListener;
private Location mLocation;
private TextView mLatitudeTextView, mLongitudeTextView;
private static final long MINIMUM_DISTANCE_FOR_UPDATES = 10; // в метрах
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 2000; // в мс
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLatitudeTextView = (TextView) findViewById(R.id.textViewLatitude);
mLongitudeTextView = (TextView) findViewById(R.id.textViewLongitude);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
String provider = mLocationManager.getBestProvider(criteria, true);
mLocation = mLocationManager.getLastKnownLocation(provider);
mLocationListener = new MyLocationListener();
showCurrentLocation(mLocation);
// Регистрируемся для обновлений
mLocationManager.requestLocationUpdates(provider,
MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_FOR_UPDATES,
mLocationListener);
}
@Override
public void onPause() {
super.onPause();
mLocationManager.removeUpdates(mLocationListener);
}
public void onClick(View v) {
showCurrentLocation(mLocation);
}
protected void showCurrentLocation(Location location) {
if (location == null) {
mLatitudeTextView.setText("Не работает");
mLongitudeTextView.setText("Не работает");
}
if (location != null) {
mLatitudeTextView.setText(String.valueOf(location.getLatitude()));
mLongitudeTextView.setText(String.valueOf(location.getLongitude()));
}
}
А вот второй:
public class MainActivity extends Activity {
double lat = 57.9989369;
double lng = 56.2853801;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myLatitude = (TextView) findViewById(R.id.latitude);
TextView myLongitude = (TextView) findViewById(R.id.longitude);
TextView myAddress = (TextView) findViewById(R.id.address);
myLatitude.setText("Широта: " + String.valueOf(lat));
myLongitude.setText("Долгота: " + String.valueOf(lng));
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder(
"Адрес:\n");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress
.append(returnedAddress.getAddressLine(i)).append(
"\n");
}
myAddress.setText(strReturnedAddress.toString());
} else {
myAddress.setText("Нет адресов!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Не могу получить адрес!");
}
}
}
Источник: Stack Overflow на русском