How can I convert Google Maps plus codes to Lng & Lat coordinates
Asked Answered
C

3

5

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

Chartres answered 23/5, 2019 at 14:38 Comment(2)
Where is your code and what have you tried?Remillard
I don't have the android app code right now becasue its on my other computer and I'm far away from home but I'm working on a web dashboard that retrives lat lng coordinates and put them in a google maps link like this : www . google .com / maps / place /@lng,lat yet it still very unaccurate and apparently plus codes are much easier and more easy to use to generate a working google maps postion linkChartres
L
5

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)
Lowpitched answered 27/5, 2019 at 19:13 Comment(0)
M
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]
Mathison answered 27/4, 2022 at 3:0 Comment(0)
T
1

Python solution:

>>> import openlocationcode
>>> openlocationcode.decode('8F6CCQCW+2F')
[34.42, 8.796125, 34.420125, 8.79625, 34.4200625, 8.7961875, 10]
Tindle answered 8/7, 2020 at 14:53 Comment(1)
sorry to say but its showing this error <br> openlocationcode.decode('8F6CCQCW+2F') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'openlocationcode' has no attribute 'decode'Keim

© 2022 - 2024 — McMap. All rights reserved.