List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); if(addresses != null) { Address returnedAddress = addresses.get(0); StringBuilder strReturnedAddress = new StringBuilder("Address:\n"); for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); strReturnedAddress.append(returnedAddress.getLocality()).append("\n"); strReturnedAddress.append(areturnedAddress.getPostalCode()).append("\n"); strReturnedAddress.append(returnedAddress.getCountryName()).append("\n"); } myAddress.setText(strReturnedAddress.toString());
public static String getAddress(Context context ,String latitude , String longitude) { Geocoder geocoder; List<Address> addressList; StringBuilder addressBuilder = new StringBuilder(); geocoder = new Geocoder(context,Locale.getDefault()); try{ addressList = geocoder.getFromLocation(Double.valueOf(latitude),Double.valueOf(longitude),1); Address address = addressList.get(0); addressBuilder.append(address.getAddressLine(0)).append(",").append(address.getLocality()).append(",") .append(address.getAdminArea()).append(",").append(address.getCountryName()).append("||").append(address.getCountryName()); }catch (Exception ex) { ex.printStackTrace(); } return addressBuilder.toString(); }
Geocoder myLocation = new Geocoder(context, Locale.ENGLISH); List<Address> myList= myLocation.getFromLocation(lat, lng, 1); Address add = myList.get(0); String addressString = add.getAddressLine(0); String country = add.getCountryName(); String city = add.getLocality();
Geocoder geocoder = new Geocoder(context, Locale.getDefault()); Address address = geocoder.getFromLocation(lat, lng, 1); Address obj = addresses.get(0); latLng = new LatLng(obj.getLatitude(), obj.getLongitude()); final String addressText = String.format("%s, %s",obj.getMaxAddressLineIndex() > 0 ? obj.getAddressLine(0) : "", obj.getCountryName());
public String getAddress(double latitude, double longitude){ if (Geocoder.isPresent()) { try { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1); if(addresses != null && addresses.size() > 0) { Address first = addresses.get(0); StringBuilder sb = new StringBuilder(); if(first.getMaxAddressLineIndex() > 0){ sb.append(first.getAddressLine(0) + ", "); } sb.append(first.getLocality() + ", "); sb.append(first.getCountryName()); return sb.toString(); } catch (Exception e) { Log.e("MyAPP", "Reverse geo lookup failed", e); return "Unavailable" } } else { return "unavailable"; } }
For user location you can use Reverse Geocoding For it u have to send only lat,long. Code is below:- public String getAddress(double lat, double lng,Context mContext) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); try { List<Address> addresses = geocoder.getFromLocation(lat, lng,1); String add=""; for(int i=0;i<addresses.size();i++){ Address obj = addresses.get(i); //String = obj.getAddressLine(i); add = add+obj.getAddressLine(i)+","+obj.getLocality()+","+obj.getAdminArea()+","+obj.getCountryName(); Log.v("IGA", "\n"+"Address " + add); } return add; } catch (IOException e) { e.printStackTrace(); Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show(); return null; } }
Address address = (Address) gotAddresses.get(0); LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude()); String properAddress = String.format("%s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getCountryName()); mMap.addMarker(new MarkerOptions() .position(new LatLng(address.getLatitude(), address.getLongitude())).draggable(true) .title(properAddress) .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
private class ReverseGeoCoding extends AsyncTask<LatLng, Void, String> { Context mContext; public ReverseGeoCoding(Context context){ super(); mContext = context; } // Finding address using reverse geocoding @Override protected String doInBackground(LatLng... params) { Geocoder geocoder = new Geocoder(mContext); double latitude = params[0].latitude; double longitude = params[0].longitude; List<Address> addresses = null; String addressText=""; try { addresses = geocoder.getFromLocation(latitude, longitude,1); } catch (IOException e) { e.printStackTrace(); } if(addresses != null && addresses.size() > 0 ){ Address address = addresses.get(0); addressText = String.format("%s, %s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getLocality(), address.getCountryName()); } return addressText; }
private MyLocation getCompleteAddressString(double LATITUDE, double LONGITUDE, int x) { String strAdd = ""; Geocoder geocoder = new Geocoder(this, Locale.getDefault()); MyLocation myLocation = new MyLocation(); //added try { List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); if (addresses != null) { Address returnedAddress = addresses.get(0); StringBuilder strReturnedAddress = new StringBuilder(""); myLocation.country = returnedAddress.getCountryName(); //added for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); } strAdd = strReturnedAddress.toString(); myLocation.address = strAdd; //added } } catch (Exception e) { lblAddress.setText("Your address cannot be determined"); } return myLocation; //modified }
address.getMaxAddressLineIndex() > 0 ? address .getAddressLine(0) : "", address .getCountryName());
address.getCountryName());
address.getCountryName());
public static String getAddressFromLocation(final Location location, final Context context, final Handler handler) { Thread thread = new Thread() { @Override public void run() { Geocoder geocoder = new Geocoder(context, Locale.getDefault()); String result = null; try { List<Address> list = geocoder.getFromLocation( location.getLatitude(), location.getLongitude(), 1); if (list != null && list.size() > 0) { Address address = list.get(0); // sending back first address line and locality result = address.getAddressLine(0) + ", " + address.getLocality()+","+address.getCountryName(); } } catch (IOException e) { Log.e(TAG, "Impossible to connect to Geocoder", e); } } }; thread.start(); return result; }
address.getCountryName());
address.getMaxAddressLineIndex() > 0 ? address .getAddressLine(0) : "", address.getLocality(), address.getCountryName());
Geocoder gcd = new Geocoder(this, Locale.getDefault()); List<Address> addresses = null; Address addr = null; try { addresses = gcd.getFromLocation(latitude, longitude, 1); if (addresses != null && addresses.size() > 0) { addr = addresses.get(0); String info = "Address is: "; info += addr.getMaxAddressLineIndex() > 0 ? addr .getAddressLine(0) : ""; info = info + ", " + addr.getLocality() + ", " + addr.getCountryName(); Toast.makeText(getApplicationContext(), info, Toast.LENGTH_LONG).show(); } else Toast.makeText(getApplicationContext(), "Address not found", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Address not found", Toast.LENGTH_LONG).show(); }
protected void search(List<Address> addresses) { Address address = (Address) addresses.get(0); home_long = address.getLongitude(); home_lat = address.getLatitude(); latLng = new LatLng(address.getLatitude(), address.getLongitude()); addressText = String.format( "%s, %s", address.getMaxAddressLineIndex() > 0 ? address .getAddressLine(0) : "", address.getCountryName()); markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.title(addressText); map1.clear(); map1.addMarker(markerOptions); map1.moveCamera(CameraUpdateFactory.newLatLng(latLng)); map1.animateCamera(CameraUpdateFactory.zoomTo(15)); locationTv.setText("Latitude:" + address.getLatitude() + ", Longitude:" + address.getLongitude()); }
addressText = String.format("%s, %s, %s", bestMatch .getMaxAddressLineIndex() > 0 ? bestMatch.getAddressLine(0) : "", bestMatch.getLocality(), bestMatch.getCountryName());
if(state==null) state=""; String country=addre.getCountryName(); if(country==null) country="";
address.getMaxAddressLineIndex() > 0 ? address .getAddressLine(0) : "", address.getLocality(), address.getCountryName());