How To Get A Currency Symbol Based On Gps Location Or Country Code
I have implemented a mini project and there i need to display currency symbol based on Location. ex: If i am in India it should display rupee symbol if USA $ symbol, I have implem
Solution 1:
I needed to show currency symbol of respective country and get country are from GPS i searched lot finally came up with below code its working properly so i want to share this code others could not waste there time
here you need first country code like IN,US from latitude longitude we get full address
Please check GPS permission in manifest file before run code. below are the some permissions
need GPSTracker.java file code create GPSTracker java file and write below code. in this some red line dont worry it affect nothing
publicclassGPSTrackerextendsServiceimplementsLocationListener {
privatefinal Context mContext;
// Flag for GPS statusbooleanisGPSEnabled=false;
// Flag for network statusbooleanisNetworkEnabled=false;
// Flag for GPS statusbooleancanGetLocation=false;
Location location; // Locationdouble latitude; // Latitudedouble longitude; // Longitude// The minimum distance to change Updates in metersprivatestaticfinallongMIN_DISTANCE_CHANGE_FOR_UPDATES=10; // 10 meters// The minimum time between updates in millisecondsprivatestaticfinallongMIN_TIME_BW_UPDATES=1000 * 60 * 1; // 1 minute// Declaring a Location Managerprotected LocationManager locationManager;
publicGPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// Getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// No network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// If GPS enabled, get latitude/longitude using GPS Servicesif (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app.
* */publicvoidstopUsingGPS() {
if (locationManager != null) {
//locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */publicdoublegetLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitudereturn latitude;
}
/**
* Function to get longitude
* */publicdoublegetLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitudereturn longitude;
}
/**
* Function to check GPS/Wi-Fi enabled
* @return boolean
* */publicbooleancanGetLocation() {
returnthis.canGetLocation;
}
/**
* Function to show settings alert dialog.
* On pressing the Settings button it will launch Settings Options.
* */publicvoidshowSettingsAlert() {
AlertDialog.BuilderalertDialog=newAlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing the Settings button.
alertDialog.setPositiveButton("Settings", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
Intentintent=newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// On pressing the cancel button
alertDialog.setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@
Override
publicvoidonLocationChanged(Location location) {}
@
Override
publicvoidonProviderDisabled(String provider) {}
@
Override
publicvoidonProviderEnabled(String provider) {}
@
Override
publicvoidonStatusChanged(String provider, int status, Bundle extras) {}
@
Override
public IBinder onBind(Intent arg0) {
returnnull;
}
}
here is mainactivity.java class
publicclassMainActivityextendsAppCompatActivity {
publicstatic SortedMap < Currency, Locale > currencyLocaleMap;
TextView t;
Geocoder geocoder;
privatestaticfinal Map < String, Locale > COUNTRY_TO_LOCALE_MAP = newHashMap < String, Locale > ();
static {
Locale[] locales = Locale.getAvailableLocales();
for (Locale l: locales) {
COUNTRY_TO_LOCALE_MAP.put(l.getCountry(), l);
}
}
publicstatic Locale getLocaleFromCountry(String country) {
return COUNTRY_TO_LOCALE_MAP.get(country);
}
StringCurrencysymbol="";
@
Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.text);
GPSTrackergpsTracker=newGPSTracker(MainActivity.this);
geocoder = newGeocoder(MainActivity.this, getLocaleFromCountry(""));
doublelat= gpsTracker.getLatitude();
doublelng= gpsTracker.getLongitude();
Log.e("Lat long ", lng + "lat long check" + lat);
currencyLocaleMap = newTreeMap < Currency, Locale > (newComparator < Currency > () {
publicintcompare(Currency c1, Currency c2) {
return c1.getCurrencyCode().compareTo(c2.getCurrencyCode());
}
});
for (Locale locale: Locale.getAvailableLocales()) {
try {
Currencycurrency= Currency.getInstance(locale);
currencyLocaleMap.put(currency, locale);
Log.d("locale utill", currency + " locale1 " + locale.getCountry());
} catch (Exception e) {
Log.d("locale utill", "e" + e);
}
}
try {
List < Address > addresses = geocoder.getFromLocation(lat, lng, 2);
Addressobj= addresses.get(0);
Currencysymbol = getCurrencyCode(obj.getCountryCode());
Log.e("getCountryCode", "Exception address " + obj.getCountryCode());
Log.e("Currencysymbol", "Exception address " + Currencysymbol);
} catch (Exception e) {
Log.e("Exception address", "Exception address" + e);
// Log.e("Currencysymbol","Exception address"+Currencysymbol);
}
t.setText(Currencysymbol);
}
public String getCurrencyCode(String countryCode) {
Strings="";
for (Locale locale: Locale.getAvailableLocales()) {
try {
if (locale.getCountry().equals(countryCode)) {
Currencycurrency= Currency.getInstance(locale);
currencyLocaleMap.put(currency, locale);
Log.d("locale utill", currency + " locale1 " + locale.getCountry() + "s " + s);
s = getCurrencySymbol(currency + "");
}
} catch (Exception e) {
Log.d("locale utill", "e" + e);
}
}
return s;
}
public String getCurrencySymbol(String currencyCode) {
Currencycurrency= Currency.getInstance(currencyCode);
System.out.println(currencyCode + ":-" + currency.getSymbol(currencyLocaleMap.get(currency)));
return currency.getSymbol(currencyLocaleMap.get(currency));
}
}
Solution 2:
You can use this to get locale of your Location
Locale locale= getResources().getConfiguration().locale;
Currency currency=Currency.getInstance(locale);
Stringsymbol= currency.getSymbol();
Refernce : Getting Current Locale
Post a Comment for "How To Get A Currency Symbol Based On Gps Location Or Country Code"