null
null

Reputation: 31

How to get a full code from open location code

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);
        }

Upvotes: 2

Views: 3466

Answers (2)

Doug Rinckes
Doug Rinckes

Reputation: 36

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).

Upvotes: 2

rednuht
rednuht

Reputation: 1812

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

Upvotes: 1

Related Questions