You can convert Plus Codes into lat/lng
format via Plus codes API. If you have full (8-digits before "+" character, e.g. 8F6CCQCW+2F
for your location) Plus Code, you can locally (without any internet request) use OpenLocationCode.decode()
method this way:
...
OpenLocationCode olc = new OpenLocationCode("8F6CCQCW+2F");
Log.d(TAG, "Lat = " + olc.decode().getCenterLatitude() + " Lng = " + olc.decode().getCenterLongitude());
...
If you have short Plus Code (less than 8 digits before "+" character, e.g. CCQCW+2F Gafsa
for your location, Gafsa is the area and it's used instead of using the full plus code) you can use HttpURLConnection
with
`https://plus.codes/api?address=CCQCW%2B2F Gafsa&key=YOUR_GEOCODING_API_KEY`
(%2B
is for the for +
symbol)
(NB! you need Geocoding API Key for geocode Gafsa
part, which is the area, you need an area with a short plus code)
and get location.lat
and location.lng
tags from its JSON response:
{
"plus_code": {
"global_code": "8F6CCQJG+",
"geometry": {
"bounds": {
"northeast": {
"lat": 34.432500000000005,
"lng": 8.777500000000003
},
"southwest": {
"lat": 34.43000000000001,
"lng": 8.775000000000006
}
},
"location": {
"lat": 34.431250000000006,
"lng": 8.776250000000005
}
},
"locality": {}
},
"status": "OK"
}
For "alternative" (saving my geolocation in a plus codes format) you can (fully local) use encode()
method of OpenLocationCode
class:
OpenLocationCode.encode(34.43125, 8.77625)