How to get country name in Android, If are known geo coordinates? How to make it the simplest way?
Get country from coordinates?
Asked Answered
There are several ways of doing it: #6922812 –
Hamate
Use Below Function for that.
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
GUIStatics.currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
GUIStatics.latitude = obj.getLatitude();
GUIStatics.longitude = obj.getLongitude();
GUIStatics.currentCity= obj.getSubAdminArea();
GUIStatics.currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Add Below Permission to your manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Your code got a bug. Sometimes, from the
lat
and lng
, you got no address. Which means address.get(0)
raises IndexOutOfBound
exception. –
Gastongastralgia I would a faster way to get just country code (I don't need the full address), but here
Geocoder
works slow –
Tenfold Here you go
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
{
String countryName=addresses.get(0).getCountryName();
}
Use Below Function for that.
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
GUIStatics.currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
GUIStatics.latitude = obj.getLatitude();
GUIStatics.longitude = obj.getLongitude();
GUIStatics.currentCity= obj.getSubAdminArea();
GUIStatics.currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Add Below Permission to your manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Your code got a bug. Sometimes, from the
lat
and lng
, you got no address. Which means address.get(0)
raises IndexOutOfBound
exception. –
Gastongastralgia I would a faster way to get just country code (I don't need the full address), but here
Geocoder
works slow –
Tenfold use this
try {
Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.isEmpty()) {
placeName.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
placeName.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
}
}
}
catch(Exception e){
Toast.makeText(this, "No Location Name Found", 600).show();
}
use this in manifest file
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
How I did it recently was to read the data from the Web API URL and parse the JSON.
An example URL for the point(40.714224, -73.961452) is:
http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&oe=utf8&sensor=true_or_false&key=your_api_key
Which produces the following output:
{
"name": "40.714224,-73.961452",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [ {
"id": "p1",
"address": "285 Bedford Ave, Brooklyn, NY 11211, USA",
"AddressDetails": {
"Accuracy" : 8,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "NY",
"SubAdministrativeArea" : {
"Locality" : {
"DependentLocality" : {
"DependentLocalityName" : "Williamsburg",
"PostalCode" : {
"PostalCodeNumber" : "11211"
},
"Thoroughfare" : {
"ThoroughfareName" : "285 Bedford Ave"
}
},
"LocalityName" : "Brooklyn"
},
"SubAdministrativeAreaName" : "Kings"
}
},
"CountryName" : "USA",
"CountryNameCode" : "US"
}
},
"ExtendedData": {
"LatLonBox": {
"north": 40.7154779,
"south": 40.7127799,
"east": -73.9600584,
"west": -73.9627564
}
},
"Point": {
"coordinates": [ -73.9614074, 40.7141289, 0 ]
}
} ]
}
I find GSON to be quite good for parsing JSON in Android.
Here is an approach that also works offline and does not cost API money:
import io.github.coordinates2country.Coordinates2Country;
[...]
Coordinates2Country.country(-23.7, 39.8); // returns the String "France".
You will need to import the library, for instance with Gradle:
implementation("io.github.coordinates2country:coordinates2country:1.7")
- Open source.
- You get the result in only 50 milliseconds, faster than any API.
- The JAR is less than 100kB.
- Disclaimer: Made by me.
https://github.com/coordinates2country/coordinates2country-android
© 2022 - 2025 — McMap. All rights reserved.