Flutter:如何将经纬度转换为加码打开位置码,反之亦然

问题描述

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

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

我坚持这种转换。

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

解决方法

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

要做到这一点,您可以像这样在 int arr2[10] 中添加它:

pubspec.yaml

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

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

要将 lat&lon 转换为 plus 代码,您可以这样做(创建 CodeArea 对象):

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

要将 plus 代码解码为经纬度,您可以这样做:

// 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'

为了访问 lat&lon,您必须执行以下操作:

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)

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

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

希望以上内容会有所帮助。这是一个 detailed explanation