'How can I convert Google Maps plus codes to Lng & Lat coordinates

I'm working on a project and I'm using some old google maps stuff. It requires longitude and latitude gps coordinates to get the location of a hospital for example.

I can't find a way to retrieve that info in this format 'lng,lat'

For example: (34.417514, 8.7518123)

The only thing the new google maps has is plus codes (used by https://plus.codes/) which they use as the location coordinates.

Can you guys help me find out a way to convert them to lng lat format or a way to retrieve the lng and lat from Google Maps website directly.

Or an alternative by saving my geolocation in a plus codes format in android studio (in a database of course) instead of using lat lng

https://www.up-00.com/i/00125/m4kadq6c7aeb.png



Solution 1:[1]

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)

Solution 2:[2]

Python solution:

>>> import openlocationcode
>>> openlocationcode.decode('8F6CCQCW+2F')
[34.42, 8.796125, 34.420125, 8.79625, 34.4200625, 8.7961875, 10]

Solution 3:[3]

Python implementation

from openlocationcode import openlocationcode as olc 
olc.decode("8F6CCQCW+2F")

Output:

[34.42, 8.796125, 34.420125, 8.79625, 34.4200625, 8.7961875, 10]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 hoju
Solution 3 Aeon