I've implemented a web-based, JSON API to convert from locations to plus codes and back. I'm announcing it here in the hopes some of you will try it out and let me know what you think.
To encode a location, make a request with the latitude and longitude:
This will return a JSON object:
"global_code": "796RWF8Q+WF",
"lat": 14.917375000000007,
"lng": -23.511250000000018
"lat": 14.91725000000001,
"lng": -23.511375000000015
If you have a code and you want to decode it, make a request with the code (you must correctly escape the "+" sign):
The result will be the same as the encode result, with the full code and the coordinates of the bounding box.
If you have a shortened code, and need to recover the original global code relative to a location (say, the user's current position), use:
This will find the nearest WF8Q+WF to the passed lat/lng and decode it, returning the same JSON object as above.
If you want to convert the code_area bounding box to a single lat/lng, say to mark it on a map or something, compute the center like this:
var lat = (data.results[0].plus_code.code_area.northeast.lat +
data.results[0].plus_code.code_area.southwest.lat) / 2;
var lng = (data.results[0].plus_code.code_area.northeast.lng +
data.results[0].plus_code.code_area.southwest.lng) / 2;
If you have a
Google Geocoding API key, you can include it in your request (with the
key= parameter). The results will be the same that you would normally get from the geocoding API, but each result will also have the plus code information added to it. In the interests of keeping this email short, I'm not going to go into a lot of detail, so play with it and send me your impression or questions!
Finally - I need your feedback! Is this useful? Should the JSON object be formatted differently, are there fields missing or extra fields you don't need? Is anything confusing? Let me know!
Doug Rinckes
Technical Program Manager
Google Switzerland