我正在尝试从简短的开放位置代码中获取完整的开放位置代码,我做错了什么?我在我的 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);
}
最佳答案
完整代码看起来像 8FVC9G8F+6W - 也就是说,它在“+”之前有八位数字。
短代码是一样的,只是“+”前的数字更少(通常是四位)。
"QWF2+JC Hautala"是带有地区的短代码。要将其转换为完整代码,您需要:
- 将其拆分为“QWF2+JC”和“Hautala”;
- 使用您选择的地理编码器将“Hautala”地理编码为纬度和经度;
- 使用 OpenLocationCode 对象的
recover()
方法恢复完整代码。
另一种方法是将它扔到 Google Geocoding API 处(您需要获得一个 API key )。
关于java - 如何从开放位置代码中获取完整代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52833307/