Skip to content

Instantly share code, notes, and snippets.

@aftabsikander
Created July 31, 2018 10:56
Show Gist options
  • Save aftabsikander/50f02ecf0466b64ec311ad052364d40a to your computer and use it in GitHub Desktop.
Save aftabsikander/50f02ecf0466b64ec311ad052364d40a to your computer and use it in GitHub Desktop.
Reverse Geo Code
package com.boloro.boloromobileapp.util;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
/**
* Created by afali on 7/19/2016.
*/
public class GeoCoderUtil {
/***
* This method is used to get the complete address of given LatLng Position
*
* @param mContext Current Activity Context
* @param latitude Latitude point
* @param longitude longitude point
* @return return complete address string
*/
public static String getAddress(Context mContext, double latitude, double longitude, Boolean withoutLineBreak) {
StringBuilder result = new StringBuilder();
String countryName = "", countryCode = "";
try {
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
countryCode = address.getCountryCode();
countryName = address.getCountryName();
int addressLengthIndex = address.getMaxAddressLineIndex();
if (withoutLineBreak) {
if (addressLengthIndex != -1) {
for (int i = 0; i <= addressLengthIndex; i++) {
if (i == addressLengthIndex)
result.append(address.getAddressLine(i));
else
result.append(address.getAddressLine(i) + ", ").append("\n");
}
} else {
address.getFeatureName();
result.append(address.getLocality()).append(" ");
result.append(address.getCountryName());
}
} else {
if (addressLengthIndex != -1) {
for (int i = 0; i <= addressLengthIndex; i++) {
if (i == addressLengthIndex)
result.append(address.getAddressLine(i));
else
result.append(address.getAddressLine(i) + ", ");
}
} else {
result.append(address.getLocality()).append("\n");
result.append(address.getCountryName());
}
}
// result.append(address.getSubThoroughfare() + " " + address.getThoroughfare() + " " +
// address.getSubLocality() + " " + address.getLocality() + " " + address.getCountryName());
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
return countryCode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment