【问题标题】:Flutter : How to convert to longitude and latitude to plus code (open location code) and vice versaFlutter:如何将经度和纬度转换为加码(打开位置码),反之亦然
【发布时间】:2020-12-20 06:47:54
【问题描述】:

我刚刚开发了一个使用 dart 和 Flutter 的应用程序,我可以获取该位置的纬度和经度。我的应用程序可以处理这个。但我想将此经度和纬度转换为打开位置代码(加号)并在我的应用视图中显示相应的加号

此外,如果我在第二阶段也输入加号代码,它将转换为经度和纬度。

我被这个转换困住了。

如果有人在这里帮助我,我将不胜感激。

【问题讨论】:

标签: flutter dart latitude-longitude google-location-services open-location-code


【解决方案1】:

我在pub.dev上没有找到包,所以你必须使用我上面提到的库,直接来自GitHub。它由 Google 提供,因此应该非常安全且维护良好。

为此,您将其添加到pubspec.yaml,如下所示:

dependencies:
  flutter:
     sdk: flutter  
  open_location_code:
     git:
       url: git://github.com/google/open-location-code.git
       path: dart

然后,在您的代码中导入库:

import 'package:open_location_code/open_location_code.dart' as olc;

要将 lat&lon 转换为 plus 代码,请执行以下操作(创建 CodeArea 对象):

// Googleplex
// 1600 Amphitheatre Pkwy
// Mountain View, CA 94043  
// The coordinates of Google's Global HQ  
var codeFromLatLon = olc.encode(37.425562, -122.081812, codeLength: 1000000);
print(codeFromLatLon.toString());
// The above will print '849VCWG9+67GCC32'

要将加号代码解码为纬度,请执行以下操作:

var codeFromPlusCode = olc.decode('849VCWG9+67GCC32');
print(codeFromPlusCode.toString());
// The above will print:
// CodeArea(south:37.425562, west:-122.08181201171875, north:37.42556204, east:-122.08181188964843, codelen: 15)

为了访问纬度和经度,您必须执行以下操作:

print('Lat: ${codeFromPlusCode.center.latitude}, Lon: ${codeFromPlusCode.center.longitude}');

请注意,当您在 plus 代码中编码 lat&lon 对时,您可以将长度限制为 10。这将为您提供更短的代码,但定位不太准确:

var codeFromLatLonShorter = olc.encode(37.425562, -122.081812, codeLength: 10);
print(codeFromLatLonShorter.toString());
// The above will print '849VCWG9+67'

希望以上内容对您有所帮助。这是detailed explanation

【讨论】:

    猜你喜欢
    • 2011-10-24
    • 2019-03-01
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    相关资源
    最近更新 更多