Hello! There are 2 codes. The first determines the coordinates. The second one determines the addresses of manually entered coordinates. How to combine these 2 codes to get an application in which the coordinates of the first code could get the name of the city from the second code? The main problem seems to be that in the first code the coordinates are of the String type, and in the second, the coordinates of the Double type. Tried String to translate to Double. Does not work.
Here is the first code:
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())); } }
And here is the second:
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("Не могу получить адрес!"); } } }