【问题标题】:How to get a full code from open location code如何从开放位置代码中获取完整代码
【发布时间】:2018-10-16 10:24:04
【问题描述】:

我试图从简短的开放位置代码中获取完整的开放位置代码,我做错了什么?我在我的 android 项目中使用 Java Open Location Code -library。

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

【问题讨论】:

  • 您应该查看崩溃日志以了解更多详细信息。 Unfortunately MyApp has stopped. How can I solve this?中有说明。调试总是一个好主意。您也可以输入 OpenLocationCode 源代码,看看哪里出了问题。至少有了崩溃日志,人们可以更好地帮助你。否则我们需要研究 OpenLocationCode 源代码,这要求很高。 :)
  • 对。由于某种原因,我无法编辑我的帖子。但这里是异常消息:E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: Method decode() could only be called on valid full codes, code was QWF2+JC.
  • 你可以在这里看到问题的根源:https://github.com/google/open-location-code/blob/master/java/src/main/java/com/google/openlocationcode/OpenLocationCode.java#L274。看起来短代码只是在decode() 中被拒绝,并且不适用于您要完成的任务。所以,这“按规定工作”。
  • 是的,这就是我最初询问如何获得全尺寸开放位置代码的原因,因为谷歌地图移动应用程序没有为其用户提供它(只有短 olc)。如果我的原始帖子不太清楚,请原谅我。

标签: java android coordinates open-location-code


【解决方案1】:

完整的代码看起来像 8FVC9G8F+6W - 也就是说,它在“+”之前有八位数字。

短代码是相同的,只是“+”之前的数字更少(通常是四个)。

“QWF2+JC Hautala”是一个带有局部性的短代码。要将其转换为完整代码,您需要:

  1. 将其拆分为“QWF2+JC”和“Hautala”;
  2. 使用您选择的地理编码器将“Hautala”地理编码为纬度和经度;
  3. 使用 OpenLocationCode 对象的recover() 方法恢复完整代码。

另一种方法是将其扔到Google Geocoding API(您需要获取 API 密钥)。

【讨论】:

  • 感谢您的回复,但这也不是正确的答案。我的应用程序的用户可以彼此共享他们的加码,有时他们在北欧森林深处,所以他们在那里没有互联网连接。所以想象一下,如果有互联网的用户将他的加码分享给当时有互联网但后来没有互联网的其他用户。因此,现在他尝试使用该代码从我的应用程序的地图活动中获取位置,但它不起作用,因为 Geocoder 需要一个有效的连接。 Plus-code 被宣传为无论您是在线还是离线都可以使用,那么为什么我无法在没有互联网的情况下获取我的坐标呢?
【解决方案2】:

如果没有某种地理编码,就不可能从短代码中恢复完整代码。简短形式旨在更易于人类阅读并识别已知区域内的位置。

但是,如果您从 Google 地图获取短代码,则可以改为使用经度/纬度对来完全离线生成完整代码。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多