Android Question Location: Getting country name

trueboss323

Active Member
Licensed User
Longtime User
Hello, is there a simple way to get the country name using the device's location? For example
Msgbox("You are in " & countryname &,"")
 
Last edited:

Mark Read

Well-Known Member
Licensed User
Longtime User
You could use the lon and lat coordinates (obtained from GPS, wifi or 3G) and the reverse geocode them with google.

Example:https://maps.googleapis.com/maps/api/geocode/json?latlng=47.3763900,15.0911300

will return the json below which you will need to parse.
{
"results" : [
{
"address_components" : [
{
"long_name" : "Kärntner Straße",
"short_name" : "B116",
"types" : [ "route" ]
},
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Steiermark",
"short_name" : "Steiermark",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
},
{
"long_name" : "8700",
"short_name" : "8700",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Kärntner Straße, 8700 Leoben, Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.3763849,
"lng" : 15.0911595
},
"southwest" : {
"lat" : 47.3763201,
"lng" : 15.0904825
}
},
"location" : {
"lat" : 47.3763525,
"lng" : 15.090821
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 47.3777014802915,
"lng" : 15.0921699802915
},
"southwest" : {
"lat" : 47.3750035197085,
"lng" : 15.0894720197085
}
}
},
"place_id" : "ChIJ9dGVgWrecUcRB1WP4o_d4kY",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "8700",
"short_name" : "8700",
"types" : [ "postal_code" ]
},
{
"long_name" : "Styria",
"short_name" : "Steiermark",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "8700, Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.4245145,
"lng" : 15.2322124
},
"southwest" : {
"lat" : 47.2803116,
"lng" : 15.0270734
}
},
"location" : {
"lat" : 47.345272,
"lng" : 15.1153899
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 47.4245145,
"lng" : 15.2322124
},
"southwest" : {
"lat" : 47.2803116,
"lng" : 15.0270734
}
}
},
"place_id" : "ChIJVQ0fkA3fcUcRamukvnG9ZCs",
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Leoben District",
"short_name" : "Leoben District",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Styria",
"short_name" : "Steiermark",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Leoben, Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.4243599,
"lng" : 15.23178
},
"southwest" : {
"lat" : 47.27991000000001,
"lng" : 15.02734
}
},
"location" : {
"lat" : 47.37639,
"lng" : 15.09113
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 47.4243599,
"lng" : 15.23178
},
"southwest" : {
"lat" : 47.27991000000001,
"lng" : 15.02734
}
}
},
"place_id" : "ChIJVQ0fkA3fcUcRgWOdmUutY_4",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Styria",
"short_name" : "Steiermark",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Leoben, Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.4243599,
"lng" : 15.23178
},
"southwest" : {
"lat" : 47.27991000000001,
"lng" : 15.02734
}
},
"location" : {
"lat" : 47.37639,
"lng" : 15.09113
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 47.4243599,
"lng" : 15.23178
},
"southwest" : {
"lat" : 47.27991000000001,
"lng" : 15.02734
}
}
},
"place_id" : "ChIJVQ0fkA3fcUcRCl91_0c2Ppw",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"address_components" : [
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "postal_town" ]
},
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Leoben",
"short_name" : "Leoben",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Styria",
"short_name" : "Steiermark",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
},
{
"long_name" : "8700",
"short_name" : "8700",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Leoben, 8700 Leoben, Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.4245145,
"lng" : 15.2322124
},
"southwest" : {
"lat" : 47.2803116,
"lng" : 15.0270734
}
},
"location" : {
"lat" : 47.345272,
"lng" : 15.1153899
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 47.4245145,
"lng" : 15.2322124
},
"southwest" : {
"lat" : 47.2803116,
"lng" : 15.0270734
}
}
},
"place_id" : "ChIJVQ0fkA3fcUcRRx2QBD96vjA",
"types" : [ "postal_town" ]
},
{
"address_components" : [
{
"long_name" : "Leoben District",
"short_name" : "Leoben District",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Styria",
"short_name" : "Steiermark",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Leoben District, Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.63306,
"lng" : 15.23178
},
"southwest" : {
"lat" : 47.22721,
"lng" : 14.6013099
}
},
"location" : {
"lat" : 47.37639,
"lng" : 15.09113
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 47.63306,
"lng" : 15.23178
},
"southwest" : {
"lat" : 47.22721,
"lng" : 14.6013099
}
}
},
"place_id" : "ChIJOblrCtPEcUcR2IvDoMAeD-g",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Styria",
"short_name" : "Steiermark",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Styria, Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.82789,
"lng" : 16.17014
},
"southwest" : {
"lat" : 46.61163,
"lng" : 13.56417
}
},
"location" : {
"lat" : 47.3593442,
"lng" : 14.4699827
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 47.82789,
"lng" : 16.17014
},
"southwest" : {
"lat" : 46.61163,
"lng" : 13.56417
}
}
},
"place_id" : "ChIJUQItuHg1bkcRNH0i-37kBdA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 49.0206081,
"lng" : 17.1606861
},
"southwest" : {
"lat" : 46.37233579999999,
"lng" : 9.530783399999999
}
},
"location" : {
"lat" : 47.516231,
"lng" : 14.550072
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 49.0206081,
"lng" : 17.1606861
},
"southwest" : {
"lat" : 46.37233579999999,
"lng" : 9.530783399999999
}
}
},
"place_id" : "ChIJfyqdJZsHbUcRr8Hk3XvUEhA",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}

or you could use the library from Warwound

https://www.b4x.com/android/forum/threads/geocoder-library.17115/#content
 
Upvote 0
Top