1

I'm trying to get a full open location code from short open location code, what am I doing wrong? I'm using Java Open Location Code -library in my android project.

        // 63.7740574, 23.9011008

        // This is an full olc (I searched it from web)
        // input = "9GM5QWF2+JC";

        // This is an short olc (also searched from the web)
        // input = "QWF2+JC";

        // And this is an olc which is copied to clipboard from google maps application
        input = "QWF2+JC Hautala";

        boolean isFullCode = OpenLocationCode.isFullCode(input);
        boolean isShortCode = OpenLocationCode.isShortCode(input);

        if (isFullCode || isShortCode)
        {
            OpenLocationCode olc = new OpenLocationCode(input);

            Double lat = olc.decode().getCenterLatitude(); // Crashes here if we are parsing input to short code
            Double lng = olc.decode().getCenterLatitude(); // But doesn't crash if we are using full lenght code

            result = new LatLng(lat, lng);
        }
William Denniss
  • 16,089
  • 7
  • 81
  • 124
null
  • 21
  • 1
  • 3
  • You should check out the crash log for more details. There are instructions in [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). And debugging is always a great idea. You can probably enter into the OpenLocationCode sources too and see what goes wrong and where. At least with the crash log people can better help you. Otherwise we need to study the OpenLocationCode source code and that's quite much to ask. :) – Markus Kauppinen Oct 16 '18 at 11:06
  • 1
    Right. For some reason I can't edit my post. But here is the exception message: `E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: Method decode() could only be called on valid full codes, code was QWF2+JC.` – null Oct 16 '18 at 11:44
  • You can see the root of the problem here: [https://github.com/google/open-location-code/blob/master/java/src/main/java/com/google/openlocationcode/OpenLocationCode.java#L274](https://github.com/google/open-location-code/blob/master/java/src/main/java/com/google/openlocationcode/OpenLocationCode.java#L274). Looks like short codes just are rejected in `decode()` and won't work for what you are trying to accomplish. So, this "works as specified". – Markus Kauppinen Oct 16 '18 at 12:05
  • Yup, that's why I was asking originally how to get a full sized open location code, because google maps mobile app isn't providing it for its users (only short olc). Pardon me if my original post wasn't so clear. – null Oct 16 '18 at 19:15

2 Answers2

2

A full code looks like 8FVC9G8F+6W - that is, it has eight digits before the "+".

A short code is the same, just with fewer digits before the "+" (usually four).

"QWF2+JC Hautala" is a short code with a locality. To convert it to a full code, you'll need to:

  1. Split it apart, into "QWF2+JC" and "Hautala";
  2. Geocode "Hautala" to a latitude and longitude using a geocoder of your choice;
  3. Recover the full code using the recover() method of the OpenLocationCode object.

An alternative is to just throw it at the Google Geocoding API (you'll need to get an API key).

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • Thanks for reply, but this isn't correct answer either. My application's users can share their plus-codes with each others and sometimes they are so deep in the Nordic woods so they do not have internet connectivity there. So imagine if user with internet shares his plus-code to other user who has internet then but doesn't have later. So now he tries to get the location from my app's maps activity with that code, it does not work because Geocoder needs a working connection. Plus-codes are advertised to be working whether you are online or offline, so why I can't get my coords without internet? – null Oct 19 '18 at 07:00
1

It's not possible to recover a full code from a short code without some sort of geocoding. The short form is intended to be more human-readable and to identify locations within a known region.

However, if you are getting the short code from Google Maps, you can instead use the longitude/latitude pair to generate a full code completely offline.

double latitude = 63.7740574;
double longitude = 23.9011008;

OpenLocationCode olc = new OpenLocationCode(latitude, longitude, 10); // the last parameter specifies the number of digits
String code = olc.getCode(); // this will be the full code
rednuht
  • 1,724
  • 16
  • 27