List of usage examples for android.location Geocoder getFromLocation
public List<Address> getFromLocation(double latitude, double longitude, int maxResults) throws IOException
From source file:com.ashok.location_basicexamplefromgoogle.MainActivity.java
/** --------------------------------------------------- * Display Latitute, Longitude and City name using Reverse-GeoCoding */// w w w . j a v a2s.c o m protected void displayLatLong_and_streetAddr(Location mLastLocation) { Double latitude, longitude; String addressLine = "addressLine"; String cityAndState = "City&stateName"; String countryName = "CountryName"; latitude = mLastLocation.getLatitude(); longitude = mLastLocation.getLongitude(); //Get City, State anc Country name by using Reverse-Geocoding Geocoder geoCoder = new Geocoder(this, Locale.getDefault()); try {//Reverse GeoCoding - Get street-address from Latitude, Longitude List<Address> address = geoCoder.getFromLocation(latitude, longitude, 1); addressLine = address.get(0).getAddressLine(0); cityAndState = address.get(0).getAddressLine(1); countryName = address.get(0).getAddressLine(2); } catch (IOException ioe) { Log.i(TAG, "Error:ReverseGeoCoding:NetworkNotAvailable:display_lat_lng_cityName()"); mTxtError.setText("Error:ReverseGeoCoding:IOException.NetworkNotAvailable.display_lat_lng_cityName()"); ioe.printStackTrace(); } catch (IllegalArgumentException iae) { Log.i(TAG, "Error:ReverseGeoCoding:IllegalArgs:display_lat_lng_cityName()"); mTxtError.setText("Error:ReverseGeoCoding:IllegalArgs:display_lat_lng_cityName()"); iae.printStackTrace(); } mLatitudeText.setText(String.format("%s: %f", mLatitudeLabel, latitude)); mLongitudeText.setText(String.format("%s: %f", mLongitudeLabel, longitude)); mTxtAddressLine.setText(addressLine); mTxtCityAndState.setText(cityAndState); mTxtCountryName.setText(countryName); }
From source file:net.palacesoft.cngstation.client.StationActivity.java
private Address lookupAddressFromLocation(Location location) throws IOException, AddressEmptyException { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = Collections.emptyList(); if (location != null) { addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); }//from www .j av a2s . c om return extractAddress(addresses); }
From source file:com.geoffreybuttercrumbs.arewethereyet.ZonePicker.java
private void saveAlarmPoint() { Location location = zone.getLocation(); if (location == null) { Toast.makeText(this, "No location. Try again...", Toast.LENGTH_LONG).show(); return;/*from w w w.j a v a2 s .co m*/ } Geocoder geocoder = new Geocoder(this); List<Address> addresses; String address; try { addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); address = addresses.get(0).getAddressLine(0) + ", " + addresses.get(0).getLocality(); } catch (IOException e) { // Log.e("Geoffrey", "Geocoding error..."); e.printStackTrace(); address = "Unknown address"; } saveCoordinatesInPreferences((float) location.getLatitude(), (float) location.getLongitude(), zone.getRadius(), address); locationManager.removeUpdates(this); Intent bdintent = new Intent(); bdintent.setClassName("com.geoffreybuttercrumbs.arewethereyet", "com.geoffreybuttercrumbs.arewethereyet.AlarmService"); bdintent.putExtra(RADIUS, zone.getRadius()); bdintent.putExtra(LOC, location); bdintent.putExtra(TONE, uri); bdintent.putExtra(ADDRESS, address); startService(bdintent); Toast.makeText(this, "Saving Alarm...", Toast.LENGTH_LONG).show(); finish(); }
From source file:ca.cs.ualberta.localpost.view.MapsView.java
/** * Function that creates a list of addresses based on search query location * @return a list of i addresses// ww w .j a v a 2 s . co m */ private List<Address> addresses(int numberOfAddresses) { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = null; try { addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, numberOfAddresses); } catch (IOException e) { e.printStackTrace(); } return addresses; }
From source file:com.ant.sunshine.app.activities.MainActivity.java
@Override public void onUpdateLocation(Location location) { String locString = ""; Geocoder geocoder = new Geocoder(this); try {//from ww w. j a v a 2 s. co m List<Address> addressesList = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); locString = addressesList.get(0).getLocality(); locString += ", "; locString += addressesList.get(0).getCountryCode(); } catch (IOException e) { locString = null; } if (locString != null && !locString.isEmpty()) { reloadLocation(); saveStringLocationToPrefs(locString); Fragment fragment = ForecastFragment.newInstance(!mTwoPane, locString); setForecastFragment((ForecastFragment) fragment); } }
From source file:org.jraf.android.piclabel.app.form.FormActivity.java
private String reverseGeocode(float lat, float lon) { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses; try {/*from w w w . j a v a 2 s.c o m*/ addresses = geocoder.getFromLocation(lat, lon, 1); } catch (Throwable t) { Log.w(TAG, "reverseGeocode Could not reverse geocode", t); return null; } if (addresses == null || addresses.isEmpty()) return null; Address address = addresses.get(0); ArrayList<String> strings = new ArrayList<String>(5); if (address.getMaxAddressLineIndex() > 0) strings.add(address.getAddressLine(0)); if (!TextUtils.isEmpty(address.getLocality())) strings.add(address.getLocality()); if (!TextUtils.isEmpty(address.getCountryName())) strings.add(address.getCountryName()); return TextUtils.join(", ", strings); }
From source file:com.berniesanders.fieldthebern.controllers.LocationController.java
private Address getAddressForLocation(double lat, double lng) throws AddressUnavailableException { // Errors could still arise from using the Geocoder (for example, if there is no // connectivity, or if the Geocoder is given illegal location data). Or, the Geocoder may // simply not have an address for a location. In all these cases, we communicate with the // receiver using a resultCode indicating failure. If an address is found, we use a // resultCode indicating success. // The Geocoder used in this sample. The Geocoder's responses are localized for the given // Locale, which represents a specific geographical or linguistic region. Locales are used // to alter the presentation of information such as numbers or dates to suit the conventions // in the region they describe. Geocoder geocoder = new Geocoder(context, Locale.getDefault()); List<Address> addresses = null; Address address = null;/*from w ww. j a v a 2 s .co m*/ try { // Using getFromLocation() returns an array of Addresses for the area immediately // surrounding the given latitude and longitude. The results are a best guess and are // not guaranteed to be accurate. // In this sample, we get just a single address. addresses = geocoder.getFromLocation(lat, lng, 1); } catch (IOException | IllegalArgumentException ioException) { throw new AddressUnavailableException(); } // Handle case where no address was found. if (addresses == null || addresses.size() == 0) { throw new AddressUnavailableException(); } else { address = addresses.get(0); if (address == null || StringUtils.isBlank(address.getAdminArea())) { throw new AddressUnavailableException("address or state code was null/blank"); } return address; } }
From source file:com.yayandroid.utility.MapHelperFragment.java
/** * After getting myLocation, this method tries to get user's current * address. First with GeoCoder, if it cannot then it looks up googleApis to * get address online and parseS it./*from w ww.ja v a 2s . c o m*/ */ private void GetLocationInfo() { if (runningThreadGetLocationInfo || myLocation == null) return; new Thread(new Runnable() { @SuppressLint("NewApi") @Override public void run() { runningThreadGetLocationInfo = true; Address address = null; if (Build.VERSION_CODES.FROYO < Build.VERSION.SDK_INT) { if (Geocoder.isPresent()) { try { if (getActivity() != null) { Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(myLocation.getLatitude(), myLocation.getLongitude(), 1); if (addresses.size() > 0) { address = addresses.get(0); } } } catch (Exception ignored) { /* * After a while, GeoCoder start to throw * "Service not available" exception. really weird * since it was working before (same device, same * Android version etc..) */ } } } if (address != null) { // i.e., GeoCoder success parseAddressInformation(address); } else { // i.e., GeoCoder failed fetchInformationUsingGoogleMap(); } runningThreadGetLocationInfo = false; } }).start(); }
From source file:tw.com.geminihsu.app01.fragment.Fragment_Client_Service.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getCurrentGPSLocationBroadcastReceiver = new BroadcastReceiver() { @Override//from www. j a va 2 s . co m public void onReceive(Context context, Intent intent) { // if(!test) { // textView.append("\n" +intent.getExtras().get("coordinates")); if (intent.getExtras().containsKey("longitude")) { double longitude = (double) intent.getExtras().get("longitude"); double latitude = (double) intent.getExtras().get("latitude"); googleMap.clear(); setMapView(longitude, latitude); Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault()); try { List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1); if (addressList != null && addressList.size() > 0) { Address address = addressList.get(0); StringBuilder sb = new StringBuilder(); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { sb.append(address.getAddressLine(i)).append("\n"); } sb.append(address.getLocality()).append("\n"); sb.append(address.getPostalCode()).append("\n"); sb.append(address.getCountryName()); result.setLatitude(latitude); result.setLongitude(longitude); result.setCountryName(address.getCountryName()); result.setLocality(address.getLocality()); result.setZipCode(address.getPostalCode()); result.setLocation(sb.toString()); if (address.getCountryCode().equals("TW")) { result.setAddress( address.getAddressLine(0).substring(3, address.getAddressLine(0).length())); result.setLocation( address.getAddressLine(0).substring(3, address.getAddressLine(0).length())); } else { result.setAddress(sb.toString()); result.setLocation(sb.toString()); } } } catch (IOException e) { Log.e("", "Unable connect to Geocoder", e); } //test = true; } // } } }; sendDataRequest = new JsonPutsUtil(getActivity()); sendDataRequest.setServerRequestOrderManagerCallBackFunction( new JsonPutsUtil.ServerRequestOrderManagerCallBackFunction() { @Override public void createNormalOrder(NormalOrder order) { if (progressDialog_loading != null) { progressDialog_loading.cancel(); progressDialog_loading = null; } Intent intent = new Intent(getActivity(), ClientTakeRideSearchActivity.class); Bundle b = new Bundle(); b.putInt(Constants.ARG_POSITION, Integer.valueOf(order.getTicket_id())); intent.putExtras(b); startActivity(intent); //finish(); } @Override public void cancelNormalOrder(NormalOrder order) { Intent intent = new Intent(getActivity(), MainActivity.class); startActivity(intent); //finish(); } }); }
From source file:tw.com.geminihsu.app01.fragment.Fragment_Client_Service_test.java
private void getCurrentAddress(double longitude, double latitude) { Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault()); try {//w ww . j ava2 s. c o m List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1); if (addressList != null && addressList.size() > 0) { Address address = addressList.get(0); StringBuilder sb = new StringBuilder(); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { sb.append(address.getAddressLine(i)).append("\n"); } sb.append(address.getLocality()).append("\n"); sb.append(address.getPostalCode()).append("\n"); sb.append(address.getCountryName()); result.setLatitude(latitude); result.setLongitude(longitude); result.setCountryName(address.getCountryName()); result.setLocality(address.getLocality()); result.setZipCode(address.getPostalCode()); result.setLocation(sb.toString()); if (address.getCountryCode().equals("TW")) { result.setAddress(address.getAddressLine(0).substring(3, address.getAddressLine(0).length())); result.setLocation(address.getAddressLine(0).substring(3, address.getAddressLine(0).length())); } else { result.setAddress(sb.toString()); result.setLocation(sb.toString()); } curAddress = result.getAddress(); } } catch (IOException e) { Log.e("", "Unable connect to Geocoder", e); } //test = true; }